# a function that returns the number of bytes of a file

This is a discussion on a function that returns the number of bytes of a file within the C Programming forums, part of the General Programming Boards category; this function exist? how i can verify if a file is empty or not?...

1. ## a function that returns the number of bytes of a file

this function exist? how i can verify if a file is empty or not?

2. Have you tried opening the file and counting bytes as you read them?

3. how i can do this??

4. Open the file. With a while loop.
Code:
while(fgetc(file) != EOF)
{
count++;
}
~Sven

5. thx a lot

6. 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:

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;
}
Then just call it like:

Code:
long filesize;
filesize = GetFileSize("c:\\foo\\bar.ext");
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

7. ## File Positioning Functions

Originally Posted by Prelude
Have you tried opening the file and counting bytes as you read them?
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:
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. . .

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 );

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

9. >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?

10. Uh oh... cornered by a lion and a bear. (A purple lion with pink cheeks, but fierce none the less)

11. I know which side I'm standing on

12. Whose standing....I've got my popcorn ready and waiting for the show.

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

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

Page 1 of 2 12 Last