|
|
|
|
|
|
| Author |
Message |
Gordon Burditt *nix forums Guru
Joined: 02 Mar 2005
Posts: 773
|
Posted: Fri Jul 14, 2006 1:05 am Post subject:
Re: How to detect an empty file?
|
|
|
| Quote: | 1. Suppose I create a text file. Write some data to it. Then close
the FILE* pointer to this file.
2. Since I have the path for the file (as char *), I can always open
the file in append mode to write more data to it.
3. What if I want to open the file in "edit" mode, and need to insert
text at the beginning of the file?
|
There is no mode to write text to the beginning of the file *AND SHOVE
THE REST OF IT TO THE BACK OF THE FILE*. If you write a line of
50 characters, you overwrite the first 50 or so [*] bytes of the
file. If you want to prepend text to the whole file, read the file,
rewind, write the new text, then write all the old stuff.
[*] Due to differences in line endings, a line might occupy more
bytes in the file than it might appear. Consider Windows and
MS-DOS \r\n line endings.
There is no mode "wt" or "at" in standard C. Not very many systems
support on-the-fly tab expansion and compression or translation to
or from Turkish, and hopefully none of them support thermonuclear
file writes.
I think the mode you are looking for is "r+" (not "r+t"), which does
not truncate the file, but will permit fseeking and writing.
| Quote: | If I open using -
filePtr = fopen(filePath, "wt");
then the file is truncated to zero length and I loose the previous
data.
If I open using
filePtr = fopen(filePath, "at");
then the file pointer is placed at the end of the file. If I try to
this -
rewind(filePtr);
now the filePtr moves to end of the file, since it was opened in append
mode and it allow me to move back anymore.
I guess the only option left is to -
long int length = someFunctionToGetLengthOfFile(filePtr);
fseek(filePtr, -length, SEEK_SET);
|
If you're thinking of doing that after opening the file in "a" mode,
it may not work. All writes may be forced to the end of the file.
| Quote: | Could someone please tell me which function would give me the length of
the text file? Portable solutions would be best, non-portable would do
as long as they work on Linux, Unix and Solaris.
|
Non-standard: the return value of ftell() after fseek(filePtr, 0L,
SEEK_END) may return the current length of the file if the seek
offset is counted in bytes (rather than, say, bytes, sectors, tracks,
cylinders, and trains, packed into one integer in bitfields). This
should work on UNIX-like systems. It may get confusing on systems
with \r\n line endings.
Non-standard: Use fstat(fileno(filePtr), &statbuffer). This should
work on UNIX-like systems.
Gordon L. Burditt |
|
| Back to top |
|
 |
av *nix forums beginner
Joined: 07 Jun 2006
Posts: 37
|
Posted: Thu Jul 13, 2006 9:25 am Post subject:
Re: How to detect an empty file?
|
|
|
On Mon, 10 Jul 2006 14:26:27 -0700, "Dann Corbit" wrote:
| Quote: | What happens if someone else appends data after you fseek()?
I don't think that the question has an answer.
|
size of file is a function of time
for example find in the time "t" the size of a file and allocate with
malloc memory == the size of file, then load that file in that memory
could be a bug if the file is more long in the time t+1 |
|
| Back to top |
|
 |
Michael Mair *nix forums Guru
Joined: 19 Feb 2005
Posts: 963
|
Posted: Thu Jul 13, 2006 7:00 am Post subject:
Re: How to detect an empty file?
|
|
|
ritesh schrieb:
| Quote: | While going through this thread and writting some of my own code I
noticed this -
1. Suppose I create a text file. Write some data to it. Then close
the FILE* pointer to this file.
2. Since I have the path for the file (as char *), I can always open
the file in append mode to write more data to it.
3. What if I want to open the file in "edit" mode, and need to insert
text at the beginning of the file?
|
Then I'd rather open it in "r+" mode.
Note that you cannot "insert" text but that you have to read
all the file into a buffer, write the part to be inserted into
the file and then the buffer contents.
If you want to overwrite something of the same length, then
this of course is possible.
| Quote: | If I open using -
filePtr = fopen(filePath, "wt");
|
There is no 't' in standard C. You can leave it out.
| Quote: | then the file is truncated to zero length and I loose the previous
data.
If I open using
filePtr = fopen(filePath, "at");
then the file pointer is placed at the end of the file. If I try to
this -
rewind(filePtr);
now the filePtr moves to end of the file, since it was opened in append
mode and it allow me to move back anymore.
|
Nonsense. Read your C standard library reference of choice.
rewind() is in terms of positioning equivalent to
(void)fseek(stream, 0L, SEEK_SET)
| Quote: | I guess the only option left is to -
long int length = someFunctionToGetLengthOfFile(filePtr);
fseek(filePtr, -length, SEEK_SET);
|
Not at all. Read past threads on this issue.
| Quote: | Could someone please tell me which function would give me the length of
the text file?
|
C works with streams which are not only files.
There is no standard C function to do so as a stream could
have "infinite" length and/or could not be evaluated for length
as this would discard the actual "content".
| Quote: | Portable solutions would be best, non-portable would do
as long as they work on Linux, Unix and Solaris.
|
Have a look at POSIX functions and/or ask in comp.unix.programmer.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address. |
|
| Back to top |
|
 |
Keith Thompson *nix forums Guru
Joined: 28 Feb 2005
Posts: 5173
|
Posted: Thu Jul 13, 2006 6:55 am Post subject:
Re: How to detect an empty file?
|
|
|
"ritesh" <riteshkapoor@gmail.com> writes:
| Quote: | While going through this thread and writting some of my own code I
noticed this -
1. Suppose I create a text file. Write some data to it. Then close
the FILE* pointer to this file.
2. Since I have the path for the file (as char *), I can always open
the file in append mode to write more data to it.
3. What if I want to open the file in "edit" mode, and need to insert
text at the beginning of the file?
|
There's no standard way to do that, and very likely no system-specific
way to do it directly. The usual way to do that kind of thing is to
create a new file, write the new data to it, then copy the old file,
the rename the new file to the old file's name.
| Quote: | If I open using -
filePtr = fopen(filePath, "wt");
then the file is truncated to zero length and I loose the previous
data.
If I open using
filePtr = fopen(filePath, "at");
then the file pointer is placed at the end of the file. If I try to
this -
rewind(filePtr);
now the filePtr moves to end of the file, since it was opened in append
mode and it allow me to move back anymore.
|
Both "wt" and "at" are non-standard. The only standard mode arguments
for fopen() are:
r w a
rb wb ab
r+ w+ a+
r+b w+b a+b (or rb+ wb+ ab+)
| Quote: | I guess the only option left is to -
long int length = someFunctionToGetLengthOfFile(filePtr);
fseek(filePtr, -length, SEEK_SET);
Could someone please tell me which function would give me the length of
the text file? Portable solutions would be best, non-portable would do
as long as they work on Linux, Unix and Solaris.
|
The only portable way to get the length of a text file is to read the
whole file and count the characters. (On some systems, this won't be
the physical size of the file; for example, on Windows each
two-character CR LF end-of-line marker is translated to a single '\n'
-- but only in text mode.)
Non-portably, you might be able to use fseek() to seek to the end of
the file, then ftell() to find out what the offset is. There's no
guarantee that the result of ftell() is meaningful for a text file
(other than as an argument to fseek()), <OT>but it should gives you a
simple byte count on Unix-like systems.</OT>
<OT>For Unix-like systems, see also the stat() function</OT>
However, knowing the length of a text file doesn't help you insert
text at the beginning of it. You'll still need to create a new file;
there's no way to shift the existing data.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this. |
|
| Back to top |
|
 |
ritesh *nix forums beginner
Joined: 31 May 2005
Posts: 11
|
Posted: Thu Jul 13, 2006 5:28 am Post subject:
Re: How to detect an empty file?
|
|
|
While going through this thread and writting some of my own code I
noticed this -
1. Suppose I create a text file. Write some data to it. Then close
the FILE* pointer to this file.
2. Since I have the path for the file (as char *), I can always open
the file in append mode to write more data to it.
3. What if I want to open the file in "edit" mode, and need to insert
text at the beginning of the file?
If I open using -
filePtr = fopen(filePath, "wt");
then the file is truncated to zero length and I loose the previous
data.
If I open using
filePtr = fopen(filePath, "at");
then the file pointer is placed at the end of the file. If I try to
this -
rewind(filePtr);
now the filePtr moves to end of the file, since it was opened in append
mode and it allow me to move back anymore.
I guess the only option left is to -
long int length = someFunctionToGetLengthOfFile(filePtr);
fseek(filePtr, -length, SEEK_SET);
Could someone please tell me which function would give me the length of
the text file? Portable solutions would be best, non-portable would do
as long as they work on Linux, Unix and Solaris.
Thanks,
Ritesh |
|
| Back to top |
|
 |
Walter Roberson *nix forums Guru
Joined: 19 Feb 2005
Posts: 1300
|
Posted: Wed Jul 12, 2006 3:44 am Post subject:
Re: How to detect an empty file?
|
|
|
In article <w46dnVmHc8V-rCnZnZ2dnUVZ_vydnZ2d@comcast.com>,
Joe Wright <joewwright@comcast.net> wrote:
| Quote: | long sof(FILE *f) {
long end, her = ftell(f);
fseek(f, 0, SEEK_END);
end = ftell(f);
fseek(f, her, SEEK_SET);
return end;
}
|
Small problems:
1) fseek() undoes any ungetc()
2) You haven't really defined what the return value means.
If f is a text stream, then the value returned by ftell() is
opaque. Looking at the man page I have handy, I can't tell if
it is even certain to be non-zero for non-empty files
3) You don't check the return value from fseek(SEEK_END) so you
are not certain that the fseek() has succeeded. Your value
"end" might be the same as your value "her", rather than reflecting
the position of the end of file or signalling that the
position is not meaningful (e.g., for a device).
--
Is there any thing whereof it may be said, See, this is new? It hath
been already of old time, which was before us. -- Ecclesiastes |
|
| Back to top |
|
 |
Joe Wright *nix forums Guru
Joined: 22 Feb 2005
Posts: 468
|
Posted: Tue Jul 11, 2006 11:19 pm Post subject:
Re: How to detect an empty file?
|
|
|
Olivier wrote:
| Quote: | Dear all,
I thought the code
-----------------------------
pt_fichier_probleme = fopen(nom_fichier, "w");
if(pt_fichier_probleme == NULL){
message_warning_s
("Erreur l'ouverture du fichier\n%s\n", (gchar *)nom_fichier);
return;}
else {
rewind(pt_fichier_probleme); /* Be sure we're at beginning */
if(feof(pt_fichier_probleme) == 0){
/* We are not at end of buffer ... It means the
file already has some content!! */
if( AskConfirmation(user_data) == 0){
/* L'utilisateur ne veut pas qu'on ecrive sur le fichier !!*/
fclose(pt_fichier_probleme);
return;};
};};
-----------------------------
was an excellent way of
-- opening the file nom_fichier for writing,
-- detecting a mistake if it was not possible,
-- if the file was not empty, the askign the
user whether it still wants to overwrite it.
(that's AskConfirmation : a window with the question and so on)
As it turns out, confirmation is always asked :-(
Help?
Best !
Amities,
Olivier
|
long sof(FILE *f) {
long end, her = ftell(f);
fseek(f, 0, SEEK_END);
end = ftell(f);
fseek(f, her, SEEK_SET);
return end;
}
--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein --- |
|
| Back to top |
|
 |
Joe Wright *nix forums Guru
Joined: 22 Feb 2005
Posts: 468
|
Posted: Tue Jul 11, 2006 11:16 pm Post subject:
Re: How to detect an empty file?
|
|
|
Olivier wrote:
| Quote: | Dear all,
I thought the code
-----------------------------
pt_fichier_probleme = fopen(nom_fichier, "w");
if(pt_fichier_probleme == NULL){
message_warning_s
("Erreur l'ouverture du fichier\n%s\n", (gchar *)nom_fichier);
return;}
else {
rewind(pt_fichier_probleme); /* Be sure we're at beginning */
if(feof(pt_fichier_probleme) == 0){
/* We are not at end of buffer ... It means the
file already has some content!! */
if( AskConfirmation(user_data) == 0){
/* L'utilisateur ne veut pas qu'on ecrive sur le fichier !!*/
fclose(pt_fichier_probleme);
return;};
};};
-----------------------------
was an excellent way of
-- opening the file nom_fichier for writing,
-- detecting a mistake if it was not possible,
-- if the file was not empty, the askign the
user whether it still wants to overwrite it.
(that's AskConfirmation : a window with the question and so on)
As it turns out, confirmation is always asked :-(
Help?
Best !
Amities,
Olivier
|
long sof(FILE *f) {
long end, her = ftell(f);
fseek(f, 0, SEEK_END);
end = ftell(f);
fseek(f, her, SEEK_SET);
return end;
}
--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein --- |
|
| Back to top |
|
 |
void * clvrmnky() *nix forums Guru Wannabe
Joined: 01 Mar 2006
Posts: 152
|
Posted: Tue Jul 11, 2006 3:28 pm Post subject:
Re: How to detect an empty file?
|
|
|
Keith Thompson wrote:
| Quote: | "Tom St Denis" <tomstdenis@gmail.com> writes:
[...]
But even Windows has "stat" like functionality. BSD, MacOs, Linuxes
have it too. So do UNIXes. So very likely using some stat function is
better than opening the file and doing some ftell crap.
There's a big difference between supporting the stat() function as
defined by the POSIX standard, and having "stat" like functionality.
If Windows has function that's similar to stat(), but that has a
different name and/or different semantics, then that function can't be
used in portable code. ftell() can (though there are limits to what's
guaranteed for ftell()).
And there are systems other than BSD, MacOS, Linux, and Windows. Does
OpenVMS support stat()? What about IBM's various mainframe operating
systems? (Those are rhetorical questions, BTW.)
If we were looking for an answer, I can supply one. Many IBM mainframes |
only have stat() and other POSIX (or POSIX-like) routines if they are
supplied as an add-on. IBM has such routines for the 390, at least in
part, because a company I worked for supplied them.
| Quote: | Sometimes a non-portable solution is the best one, but it's important
to know what's part of the C standard and what isn't, and to know just
what tradeoffs you're making.
Point taken. |
|
|
| Back to top |
|
 |
Chris Torek *nix forums Guru
Joined: 26 Feb 2005
Posts: 481
|
Posted: Tue Jul 11, 2006 4:34 am Post subject:
Re: How to detect an empty file?
|
|
|
(More off-topic drift; I plan to stop adding to it after this )
| Quote: | Chris Torek <nospam@torek.net> writes:
... open() with O_CREAT|O_EXCL, for instance, [avoids certain races on
POSIX systems]. |
In article <lnd5ccg8kf.fsf@nuthaus.mib.org>,
Keith Thompson <kst-u@mib.org> wrote:
| Quote: | Not entirely topical, but ...
According to the Linux man page for open(2):
O_EXCL is broken on NFS file systems, programs which rely on it
for performing locking tasks will contain a race condition.
|
This is (at least theoretically) fixed in modern versions of NFS,
which use a "create verifier" to make the "idempotent but unrepeatable
due to O_EXCL" operation repeatable (hence "really" idempotent).
The client and server both have to support at least v3, and both
have to implement create-verifiers. Not all NFS-es do so, alas.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers. |
|
| Back to top |
|
 |
Keith Thompson *nix forums Guru
Joined: 28 Feb 2005
Posts: 5173
|
Posted: Tue Jul 11, 2006 2:49 am Post subject:
Re: How to detect an empty file?
|
|
|
Chris Torek <nospam@torek.net> writes:
| Quote: | Walter Roberson wrote:
Your proposed solution also would require that the user open
the file-descriptor can of worms: you could have at least said fstat()
to be closer to the normal C I/O library.
In article <1152568847.806637.186200@p79g2000cwp.googlegroups.com
Tom St Denis <tomstdenis@gmail.com> wrote:
stat() works on paths not file descriptors. If you're alluding to race
conditions all file routines have them.
Not *all*: open() with O_CREAT|O_EXCL, for instance, does not.
Indeed, this is the *only* way to guarantee that a file that did
not exist before the call is created by the call and does exist
after the call.
[...] |
Not entirely topical, but ...
According to the Linux man page for open(2):
O_EXCL is broken on NFS file systems, programs which rely on it
for performing locking tasks will contain a race condition.
It suggests an alternative, but it's even more off-topic than O_EXCL,
so I'll leave it to anyone interested in the details to look it up.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this. |
|
| Back to top |
|
 |
Keith Thompson *nix forums Guru
Joined: 28 Feb 2005
Posts: 5173
|
Posted: Tue Jul 11, 2006 1:47 am Post subject:
Re: How to detect an empty file?
|
|
|
"Old Wolf" <oldwolf@inspire.net.nz> writes:
| Quote: | attn.steven.kuo@gmail.com wrote:
Determine the difference in file position between
the start and end of the file with 'fseek' and 'ftell'.
He's opening the file in text mode, so this method won't work.
|
To be painfully precise, it's not *guaranteed* to work. (It actually
will work on some systems -- which is probably worse than not working
at all.)
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this. |
|
| Back to top |
|
 |
Chris Torek *nix forums Guru
Joined: 26 Feb 2005
Posts: 481
|
Posted: Tue Jul 11, 2006 1:38 am Post subject:
Re: How to detect an empty file?
|
|
|
| Quote: | Walter Roberson wrote:
Your proposed solution also would require that the user open
the file-descriptor can of worms: you could have at least said fstat()
to be closer to the normal C I/O library.
|
In article <1152568847.806637.186200@p79g2000cwp.googlegroups.com>
Tom St Denis <tomstdenis@gmail.com> wrote:
| Quote: | stat() works on paths not file descriptors. If you're alluding to race
conditions all file routines have them.
|
Not *all*: open() with O_CREAT|O_EXCL, for instance, does not.
Indeed, this is the *only* way to guarantee that a file that did
not exist before the call is created by the call and does exist
after the call.
Similarly, open() followed by fstat() avoids some (but not all)
races: in particular, the information you get with fstat() will
definitely refer to the underlying file, and the st_ino, st_dev,
and st_rdev fields should not change (of course the size, owner,
and permissions, for instance, could change).
| Quote: | BTW saying "stat" isn't portable is kinda moot since not all platforms
have files anyways.
|
While the latter is true, the former is an overstatement: there are
hosted systems that have "files" dealt with via fopen, but lack
the stat() family of calls (e.g., C on some Univac and IBM sysems).
| Quote: | But even Windows has "stat" like functionality. BSD, MacOs, Linuxes
have it too. So do UNIXes. So very likely using some stat function is
better than opening the file and doing some ftell crap.
|
Usually it is best to open() the file, then use fstat() if needed.
On many systems, this is essentially just as fast as using stat()
on the file, too; and if you intend to open it after getting the
result from stat(), open() followed by fstat() is faster -- often
significantly faster -- than stat() followed by open(). The reason
is that most of the work (and hence most of the time) goes to
translating path names, rather than dealing with the final on-media
entity representing the file. Doing stat(), then open(), runs the
slow code twice; doing open(), then fstat(), runs it only once.
(On vxWorks, stat() is in fact implemented as open() followed by
fstat() followed by close(). So here it is *definitely* more
efficient to open() first, then fstat().)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers. |
|
| Back to top |
|
 |
Old Wolf *nix forums Guru
Joined: 20 Feb 2005
Posts: 679
|
Posted: Tue Jul 11, 2006 1:21 am Post subject:
Re: How to detect an empty file?
|
|
|
attn.steven.kuo@gmail.com wrote:
| Quote: |
Determine the difference in file position between
the start and end of the file with 'fseek' and 'ftell'.
|
He's opening the file in text mode, so this method won't work. |
|
| Back to top |
|
 |
dcorbit@connx.com *nix forums addict
Joined: 13 Jun 2006
Posts: 92
|
Posted: Mon Jul 10, 2006 10:35 pm Post subject:
Re: How to detect an empty file?
|
|
|
"Tom St Denis" <tomstdenis@gmail.com> wrote in message
news:1152568847.806637.186200@p79g2000cwp.googlegroups.com...
| Quote: | Walter Roberson wrote:
Your proposed solution also would require that the user open
the file-descriptor can of worms: you could have at least said fstat()
to be closer to the normal C I/O library.
stat() works on paths not file descriptors. If you're alluding to race
conditions all file routines have them.
BTW saying "stat" isn't portable is kinda moot since not all platforms
have files anyways.
But even Windows has "stat" like functionality. BSD, MacOs, Linuxes
have it too. So do UNIXes. So very likely using some stat function is
better than opening the file and doing some ftell crap.
|
Even at that, I don't think that any of them (techniques like stat()) work
on any of the systems mentioned.
Windows, BSD, MacOs, Linuxes, UNIXes, etc. all allow more than one thread or
process to open a given file.
Therefore, stat(), fseek(), etc. cannot give the requested answer because it
can change between the time you ask the question and the time that you use
the information returned by the inquiry.
I think a better idea is to find out what is supposed to be accomplished and
then come up with a portable solution.
IMO-YMMV. |
|
| Back to top |
|
 |
Google
|
|
| Back to top |
|
 |
|
|
The time now is Sat Nov 22, 2008 9:33 am | All times are GMT
|
|
Actress | Personal Loans | Actress | Mobile Phones | Mortgage
|
|
Copyright © 2004-2005 DeniX Solutions SRL
|
|
|
|
Other DeniX Solutions sites:
Unix/Linux blog |
electronics forum |
medicine forum |
science forum |
|
|
Privacy Policy
|
Powered by phpBB © 2001, 2005 phpBB Group
|
|