Calculating File Size in C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • seeminsuleri
    New Member
    • Nov 2006
    • 9

    Calculating File Size in C

    hello there,
    Can anyone plz guide me how to calculate the sise of a string present in an opened file.
    we already have the file descriptor, inputFile. but when i try to do

    begin=inputFile .tellg();
    inputFile.seekg (0,ios::end);
    end=inputFile.t ellg();
    size=end-begin;

    it doesnt work in C. It does work in C++ though.
    Can u plz give me some C alternative for the above.
  • macklin01
    New Member
    • Aug 2005
    • 145

    #2
    Well, a primative but sure-fire way to get the job done is to read bytes (or some other data size) until you reach the end, and keep track. All the traditional FILE operations still go in C, right? e.g.,

    int count = 0;
    FILE* fp;
    fp = fopen( "file.txt", "r" );

    while( !feof( fp ) && fp )
    {
    unsigned char c;
    fread( &c , 1,1, fp );
    count++;
    }
    if( fp )
    { fclose( fp ); }


    Again, not elegant, but robust. :) -- Paul

    Comment

    • seeminsuleri
      New Member
      • Nov 2006
      • 9

      #3
      thanks for your help Paul,
      I was looking at a code

      fseek (pFile , 0 , SEEK_END);
      lSize = ftell (pFile);
      rewind (pFile);

      I didnt know these functions exist in C.
      I think they would do the job too.

      Originally posted by macklin01
      Well, a primative but sure-fire way to get the job done is to read bytes (or some other data size) until you reach the end, and keep track. All the traditional FILE operations still go in C, right? e.g.,

      int count = 0;
      FILE* fp;
      fp = fopen( "file.txt", "r" );

      while( !feof( fp ) && fp )
      {
      unsigned char c;
      fread( &c , 1,1, fp );
      count++;
      }
      if( fp )
      { fclose( fp ); }


      Again, not elegant, but robust. :) -- Paul

      Comment

      • macklin01
        New Member
        • Aug 2005
        • 145

        #4
        Originally posted by seeminsuleri
        thanks for your help Paul,
        I was looking at a code

        fseek (pFile , 0 , SEEK_END);
        lSize = ftell (pFile);
        rewind (pFile);

        I didnt know these functions exist in C.
        I think they would do the job too.
        Ah, that's much more elegant, and likely more efficient, too. Nice work. :) -- Paul

        Comment

        • sundarr
          New Member
          • Aug 2006
          • 9

          #5
          there is an another function
          filelength(int fd); which returns the length of the file associated with the handle fd.
          U need to include io.h b4 using this function.

          Comment

          • jeffmoretti
            New Member
            • May 2008
            • 1

            #6
            About the fseek - ftell method, I should add that I saw this on http://www.cplusplus.c om/reference/clibrary/cstdio/ftell.html

            "For text streams, the value is not guaranteed to be the exact number of bytes from the beginning of the file, but the value returned can still be used to restore the position indicator to this position using fseek."

            Thus, the value is not guaranteed. So you might want to consider a different method for calculating length of string files.

            Comment

            Working...