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;
}
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;
}
ASKER
Hi rbr,
sorry for rejecting your answer, but stat and fstat are not ANSI C ...?!
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!
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.
to Kangaroo: SEEK_END sets to the end of the file.
fstat and stat are conform to AT&T and POSIX.
ASKER
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 ...
>> 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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
btw
// is not ansi too
// is not ansi too
ASKER
Ok ;-)
struct stat stat_buf;
stat ("filename",&stat_buf);
printf ("%d",stats_buf.st_size);