Thread: size of a file

  1. #1
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946

    size of a file

    is there an easier way to figure out how big a file is other than to just read it until EOF? (in C obviously)

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >is there an easier way to figure out how big a file is other than to just read it until EOF?
    Fortunately, there is.
    Code:
    #include <stdio.h>
    
    int main ( void )
    {
      FILE *fptr;
      int filesize;
    
      fptr = fopen ( "somefile", "r" );
      if ( fptr != NULL ) {
        fseek ( fptr, 0, SEEK_END );
        filesize = ftell ( fptr );
        fclose ( fptr );
      }
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    excellent! thanks

    and now if i want to read the file from the beginning i call
    Code:
    fseek (fptr, 0, SEEK_SET);
    right?

    the reason i wanted to know the size first before i read through the whole file anyway btw is that i needed to malloc() an array of structs to read the data into.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >fseek (fptr, 0, SEEK_SET);
    That would work, I personally would use

    rewind ( fptr );

    since not only does it reset the file pointer to the beginning of the file, it clears any errors so the file is fresh for the first real use. Not like you will get many errors just by getting the size, but it never hurts to be extra careful.

    If you are using the file size to allocate memory, be sure to divide it evenly for the structures. If filesize returns 100 but there are only 10 structures of 10 bytes and you allocate an array of 100 you've wasted a lot of space.
    Code:
    array = malloc ( filesize / sizeof ( struct something ) );
    -Prelude
    Last edited by Prelude; 07-31-2002 at 10:06 PM.
    My best code is written with the delete key.

  5. #5
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by Prelude
    >fseek (fptr, 0, SEEK_SET);
    That would work, I personally would use

    rewind ( fptr );

    since not only does it reset the file pointer to the beginning of the file, it clears any errors so the file is fresh for the first real use. Not like you will get many errors just by getting the size, but it never hurts to be extra careful.

    If you are using the file size to allocate memory, be sure to divide it evenly for the structures. If filesize returns 100 but there are only 10 structures of 10 bytes and you allocate an array of 100 you've wasted a lot of space.
    Code:
    array = malloc ( filesize / sizeof ( struct something ) );
    -Prelude
    done and done

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 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
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Replies: 16
    Last Post: 11-23-2007, 01:48 PM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  5. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM