Thread: a function that returns the number of bytes of a file

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    22

    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. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Have you tried opening the file and counting bytes as you read them?
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    22
    how i can do this??

  4. #4
    Registered User 00Sven's Avatar
    Join Date
    Feb 2006
    Posts
    127
    Open the file. With a while loop.
    Code:
    while(fgetc(file) != EOF)
    {
    count++;
    }
    ~Sven
    Windows XP Home Edition - Dev-C++ 4.9.9.2
    Quote Originally Posted by "The C Programming Language" by Brian W. Kernignhan and Dennis M. Ritchie
    int fflush(FILE *stream)
    On an output stream, fflush causes any buffered but unwritten data to be written; On an input stream, the effect is undefined. It returns EOF for a write error, and zero otherwise. fflush(NULL) flushes all output streams.
    board.theprogrammingsite.com

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    22
    thx a lot

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    34
    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. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    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.*

  8. #8
    Registered User fischerandom's Avatar
    Join Date
    Aug 2005
    Location
    Stockholm
    Posts
    71

    File Positioning Functions

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

    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/

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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.

  11. #11
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Uh oh... cornered by a lion and a bear. (A purple lion with pink cheeks, but fierce none the less)
    Sent from my iPadŽ

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

  13. #13
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Whose standing....I've got my popcorn ready and waiting for the show.

  14. #14
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    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.

  15. #15
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  3. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM