this function exist? how i can verify if a file is empty or not?
Printable View
this function exist? how i can verify if a file is empty or not?
Have you tried opening the file and counting bytes as you read them?
how i can do this??
Open the file. With a while loop.~SvenCode:while(fgetc(file) != EOF)
{
count++;
}
thx a lot
I don't think it's a good idea to read a file character by character just to get the size...it's unecessary, and slow. Instead, you can use fseek() to jump to the end of the file, and then ftell() to tell you where the file position indicator is at, which gives you your file size in number of bytes. Here's a function:
Then just call it like:Code:long GetFileSize(const char* filename)
{
long size;
FILE *f;
f = fopen(filename, "rb");
if (f == NULL) return -1;
fseek(f, 0, SEEK_END);
size = ftell(f);
fclose(f);
return size;
}
The only problem I see is that ftell() returns a signed long data type, so you're probably limited to filesizes around 2 gigabytes....but otherwise it'd work just fine :pCode:long filesize;
filesize = GetFileSize("c:\\foo\\bar.ext");
To count the number of sandcorns in the hottest desert on earth, what would be the best way to do that you think, of the choices given below:Quote:
Originally Posted by Prelude
A] Let's count one sandcorn a time. . .Really simple!
B] Measure the volume of the desert with some suitable technology available, that just gives the correct answer to the question instantly. . .
If you like the (A) answer, go ahead and count the bytes as you read them!
The (B) answer may look something like this:
Code:fseek( fp, 0L, SEEK_END ); /* fseek returns non-zero on error. */
sz = ftell( fp ); /* sz is of type long */
fprintf( stdout, "Bytes in file: %i\n", sz );
How about C] Read the Link Dave gave you to tell you why fseek may not work right on text files. You see, it actually is better to count every byte. Because your magical b] may not be accurate.
Quzah.
>The (B) answer may look something like this
Riiiight. Now how are you going to explain why your little snippet is broken most of the time yet still the brilliant solution you claim it to be?
Uh oh... cornered by a lion and a bear. (A purple lion with pink cheeks, but fierce none the less)
I know which side I'm standing on :)
Whose standing....I've got my popcorn ready and waiting for the show. :D
If fseek() causes problems with text files does that mean it's not a problem with binary files? There's no reason to open a file in text mode if you want it's file size anyway.
And as for the second question, you know a file is empty if it's file size is 0.
>If fseek() causes problems with text files does that mean it's not a problem with binary files?
No, it's not guaranteed to work in any way, shape, or form. The standard makes it very clear that this trick for finding the size of a file is non-portable at best and undefined otherwise.
Then again, the question was vague as the size of a file could mean different things depending on what you want to do. Usually people want to know how many characters can be read before hitting end-of-file. In that case, the only portable way to find the size of a file is to read it and count the characters.
Since the question made no mention of OS or compiler, giving a non-portable function that returns the file size for however the function defines the size of a file is unproductive. Fortunately, nobody has attempted that yet except for fischerandom's unhelpful "suitable technology" and sandcorns.