this function exist? how i can verify if a file is empty or not?
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?
My best code is written with the delete key.
how i can do this??
Open the file. With a while loop.~SvenCode:while(fgetc(file) != EOF) { count++; }
Windows XP Home Edition - Dev-C++ 4.9.9.2
board.theprogrammingsite.comOriginally Posted by "The C Programming Language" by Brian W. Kernignhan and Dennis M. Ritchie
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 fineCode:long filesize; filesize = GetFileSize("c:\\foo\\bar.ext");
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
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: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 );
Bobby Fischer Live Radio Interviews http://home.att.ne.jp/moon/fischer/
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.
Hope is the first step on the road to disappointment.
>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?
My best code is written with the delete key.
Uh oh... cornered by a lion and a bear. (A purple lion with pink cheeks, but fierce none the less)
Sent from my iPadŽ
I know which side I'm standing on
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Whose standing....I've got my popcorn ready and waiting for the show.
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.
My best code is written with the delete key.