How to detect an empty file?

T

Tom St Denis

Olivier said:
Dear all,

I thought the code

<snip>

OT but ... just use stat(). It will tell you

a) if the file or directory exists
b) What sort of entry is it [file, directory, symlink, pipe, etc]
c) The size

Peace!

Tom
 
W

Walter Roberson

Tom St Denis said:
OT but ... just use stat(). It will tell you
a) if the file or directory exists
b) What sort of entry is it [file, directory, symlink, pipe, etc]
c) The size

stat() is not part of standard C. Neither are directories, symlinks,
pipes, "etc".
 
O

Olivier

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
 
A

attn.steven.kuo

Olivier said:
Dear all,

I thought the code


Opening the file in "w" mode truncates
the file to zero length if fopen was successful,
so there's no need to check for file size afterward.

Perhaps you were thinking of opening the file
in "r+" mode?
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 :-(

Determine the difference in file position between
the start and end of the file with 'fseek' and 'ftell'.
 
T

Tom St Denis

Walter said:
Tom St Denis said:
OT but ... just use stat(). It will tell you
a) if the file or directory exists
b) What sort of entry is it [file, directory, symlink, pipe, etc]
c) The size

stat() is not part of standard C. Neither are directories, symlinks,
pipes, "etc".

I recognized that by saying "OT" so pointing this out is redundant. No
reason I can't give the dude a three second tip. Tell the dude it's OT
and then point them in the right direction. In theory, you've not only
helped them but helped clear up the group.

Just replying to someone that has already acknowledged that it's OT
with a comment about it being OT is pathetic and lame.

Tom
 
W

Walter Roberson

Olivier said:
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!! */

Two mistakes:

1) Using "w" tells fopen() to truncate the file to zero length if it
exists. Use "r+" (or "r+b") instead.

2) feof() is not set until at least one character of I/O is attempted.
You could -try- to fseek() to the end of the file. If you do that on
a text stream, the result will be a [theoretically] opaque position,
but on a binary stream ("r+b") the fseek() result can be read off
directly in characters and so can be meaningfully compared to 0.

Note: If you need to open in binary to do the fseek() test then you
can always freopen() later without the "b" flag if you really want
to work with text instead of binary.
 
W

Walter Roberson

Walter said:
I recognized that by saying "OT" so pointing this out is redundant. No
reason I can't give the dude a three second tip. Tell the dude it's OT
and then point them in the right direction. In theory, you've not only
helped them but helped clear up the group.

Except you haven't helped the OP, because stat() does not exist in
all systems. The code the OP gave had no clue as to which operating system
was in use. For example in a number of versions of MS Windows, the
closest equivilent would be one of the _stat*() calls.

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.


When you give OT answers, you should at the very least qualify them
by naming the solution domain (e.g., "Linux 3.2", "Windows NT and
later").
 
E

Eric Sosman

Olivier wrote On 07/10/06 15:05,:
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;};
};};

Yes, it does that.
-- detecting a mistake if it was not possible,

It does that, too.
-- if the file was not empty, the askign the
user whether it still wants to overwrite it.

Too late. If the file does not exist, fopen(...,"w")
creates a new, empty file. If the file exists already,
fopen(...,"w") makes it empty. Either way, the file is
empty after fopen() succeeds, and any data it might have
contained is gone.
(that's AskConfirmation : a window with the question and so on)
As it turns out, confirmation is always asked :-(

That is because feof() does not mean what you think
it does. feof() does not ask the question "Is the stream
positioned at the end of the file?" Rather, feof() asks
"Has the end of the file been reached?" Although these
seem similar (and are related), they are not quite the
same. A stream "reaches the end" of a file not by arriving
at the e-o-f position, but by trying (and failing) to go
past that position. In other words, feof() does not predict
whether the next I/O operation would occur at e-o-f, but
instead tells why an I/O operation failed: was it because
the operation tried to go past e-o-f, or was it for some
other reason (like a disk failure)?

If your fopen() succeeds, the stream is positioned at
the beginning of the empty file (which is also the end).
The rewind() presumably "succeeds" because it has nothing
to do: the stream is already at the start of the file.
You then ask feof() whether a previous I/O operation failed
by running off the end of the file; there has been no such
failure, so feof() says "No."

C has no perfect solution to your problem. One possibility
is to use fopen(...,"r") first, just to test whether the file
exists. If this fopen() succeeds, fclose() it and ask the
user for instructions; do fopen(...,"w") only if the user
chooses to overwrite the file. This is not bullet-proof for
a number of reasons: The first attempt might fail for some
reason other than "no such file," some other program might
create the file two microseconds after your program decides
that it doesn't exist, and so on. Still, it's about the best
you can do without resorting to system-specific code.

<off-topic>

A technique that works on POSIX systems is to open() the
file using the O_EXCL flag and then use fdopen() to attach a
C I/O stream to the resulting file descriptor.

</off-topic>
 
B

Bill Pursell

Walter said:
Walter said:
Except you haven't helped the OP, because stat() does not exist in
all systems. The code the OP gave had no clue as to which operating system
was in use. For example in a number of versions of MS Windows, the
closest equivilent would be one of the _stat*() calls.

Tom's reply was more helpful than saying nothing. The OP can
certainly google for stat and fstat and at the very least discover
that they are unrelated to the platform of interest. In doing so,
the OP may discover that the platform of interest does contain
a stat()-like call of which he was formerly unaware. It is
entirely likely that the OP simply is not aware that functionality
such as stat is available, so Tom's answer is completely
appropriate.
When you give OT answers, you should at the very least qualify them
by naming the solution domain (e.g., "Linux 3.2", "Windows NT and
later").

Just out of curiousity, what is Linux 3.2? kernel.org shows 2.6.17.4
as
the most recent stable release...surely you're not suggesting that
the OP use a radically unstable and indeed unheard of kernel?
 
K

Keith Thompson

Tom St Denis said:
Olivier said:
Dear all,

I thought the code

<snip>

OT but ... just use stat(). It will tell you

a) if the file or directory exists
b) What sort of entry is it [file, directory, symlink, pipe, etc]
c) The size

I see you've pointed out that this is off-topic (assuming that the
original poster understands that OT means "off-topic"), but you
haven't given a clue about *why* it's off-topic.

The stat() function is not part of the standard C library. It's
defined by POSIX. It should be available on all Unix and Unix-like
systems, and it's likely to be available on many other systems as well
(probably including Windows, but I'm not certain) -- but any code that
uses it will be less portable than code that restricts itself to the
standard C library. The loss of portability may or may not be an
acceptable price to pay for the convenience of using the stat()
function.

It's also important to remember that any function that gives you
information about a file can only give you information as of the time
it's called. It's entirely possible that you could call stat(), find
out that the file doesn't exist, and then try to create it, only to
find out that something else created the file between the stat() call
and the fopen() call. It may or may not be acceptable to ignore this
possibility.
 
O

Olivier

[...]
C has no perfect solution to your problem. One possibility
is to use fopen(...,"r") first, just to test whether the file
exists. If this fopen() succeeds, fclose() it and ask the
user for instructions; do fopen(...,"w") only if the user
chooses to overwrite the file.

Thanks to all of you. That's the solution I've finally chosen
after having had a nice but aborted trip towards <sys/stat.h>
I just couldn't make that work. That's most probably for when
I'll be older :p

Best !
Amities,
Olivier
 
S

solariun

So.. how are you don´t want to use a beatiful way (stat) try it:

fopen

x = lseek (pFile, 0, SEEK_END);

and test if x is igual top zero ou -1 (in case of error). But if zero
is returned, it´s mean that file is zero... This example can work in
every where where have a full implamentation of C standards but i will
warning you, it´s a ugly way to write this kind of procedure because
even you are using a M$ Visual C++, you can use stast. unless you are
using a DOS Turbo C of corse.

Have a good trip through it!

Olivier escreveu:
 
K

Keith Thompson

So.. how are you don´t want to use a beatiful way (stat) try it:

fopen

x = lseek (pFile, 0, SEEK_END);

and test if x is igual top zero ou -1 (in case of error). But if zero
is returned, it´s mean that file is zero... This example can work in
every where where have a full implamentation of C standards but i will
warning you, it´s a ugly way to write this kind of procedure because
even you are using a M$ Visual C++, you can use stast. unless you are
using a DOS Turbo C of corse.

lseek() is not a standard C function. You're probably thinking of
fseek().

Note that the standard makes fewer guarantees about the behavior of
fseek() than you might expect. In particular:

For a text stream, the offset argument needs to be either zero or
the result of an earlier call to ftell().

For a binary stream, fseek() with SEEK_END isn't guaranteed to be
meaningful. (An implementation may pad a binary file with bytes
with zero values, but the standard doesn't even make the
guarantees that that might imply for fseek().)

See the C standard (or n1124.pdf) for details.

It's possible that you can get away with assuming more than the
standard guarantees (<OT>for example, on a Unix system, file offsets
are simple byte counts for both text and binary files</OT>), but by
depending on these things you make your code potentially non-portable.

The obvious way to tell whether a file is empty is to attempt to read
a byte from it; if the attempt fails (e.g., fgetc() returns EOF), then
there was nothing to read -- or there was an error. (You can use
feof() and/or ferror() *after* this to determine why the attempt
failed.)
 
E

Eric Sosman

So.. how are you don´t want to use a beatiful way (stat) try it:

fopen

x = lseek (pFile, 0, SEEK_END);

and test if x is igual top zero ou -1 (in case of error). But if zero
is returned, it´s mean that file is zero... This example can work in
every where where have a full implamentation of C standards but i will
warning you, it´s a ugly way to write this kind of procedure because
even you are using a M$ Visual C++, you can use stast. unless you are
using a DOS Turbo C of corse.

This is nonsense. lseek() is not part of the C Standard
library, and a "full implamantation [sic] of C standards" need
not provide lseek().

<off-topic>

POSIX systems provide an lseek(), but it does not solve
the O.P.'s problem. For one thing, its first argument is not
a FILE*, as the code snippet above suggests. For another, it
does magically go back in time to undo fopen()'s truncation
of a pre-existing file. For a third, it still doesn't address
the issue of other programs creating files just after you've
decided they don't exist.

POSIX has ways to solve the problem, but lseek() is not
part of the solution.

</off-topic>
 
D

Dann Corbit

What happens if someone else appends data after you fseek()?

I don't think that the question has an answer.
 
T

Tom St Denis

Walter said:
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.

Tom
 
K

Keith Thompson

Tom St Denis said:
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.

Not at all. There's a very real difference between functions that are
defined in the C standard (and are therefore guaranteed to be
available on all conforming hosted implementations) and functions that
are defined in some other standard (and are therefore guaranteed to be
available only on implementations that conform to that standard).
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.)

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.
 
D

Dann Corbit

Tom St Denis said:
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.
 
O

Old Wolf

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top