size of a file

This is a discussion on size of a file within the C Programming forums, part of the General Programming Boards category; is there an easier way to figure out how big a file is other than to just read it until ...

  1. #1
    moi
    moi is offline
    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,796
    >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
    moi
    moi is offline
    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,796
    >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 11:06 PM.
    My best code is written with the delete key.

  5. #5
    moi
    moi is offline
    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, 08:54 AM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 01: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, 05:54 PM
  5. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 08:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21