Link to home
Start Free TrialLog in
Avatar of nil092297
nil092297

asked on

file length ?

is this the 'right' way to get the length of a file ?

long GetSize( FILE * pFile )
{
       long nPos;
       long nPosOld;
       
       // get old file pointer postion
       nPosOld = ftell( pFile );
       fseek( pFile, 0L, SEEK_END );
       // get end position
       nPos = ftell( pFile );
       // restore old file pointer postion
       fseek( pFile, nPosOld, SEEK_SET );
     
       return nPos;
}
 
Avatar of rbr
rbr

yes this is one way to get the file length. Another way is to use stat or fstat.

struct stat stat_buf;

stat ("filename",&stat_buf);
printf ("%d",stats_buf.st_size);


Avatar of nil092297

ASKER

Hi rbr,
sorry for rejecting your answer, but stat and fstat are not ANSI C ...?!
I'am sorry but
    fseek( pFile, 0L, SEEK_END );
will position the file pointer at the beginning of the file. So the answer to your Q is
  No!
Oeps. My fault, read the manual wrong. rbr is right it is correct. fseek positions relative the last argument.
even this is not ansi c I said that you function will do it, so I answered your question.
to Kangaroo: SEEK_END sets to the end of the file.
fstat and stat are conform to AT&T and POSIX.
Sorry again ...
>>  I said that you function will do it,
I KNOW that my function will do it ... the function works fine!
What I want to know: Is this the 'right' , the 'proper' way to do this or is there a (faster?, less function calls?) alternative  ...
 
ASKER CERTIFIED SOLUTION
Avatar of rbr
rbr

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
btw
// is not ansi too
Ok ;-)