return the number of bytes in an open file
#include <unistd.h> long int filelength( int filedes );
The filelength() function returns the number of bytes in the opened file indicated by the file descriptor filedes.
If an error occurs, (-1L) is returned, and errno is set to a value that indicates the type of error that's been detected.
Otherwise, the number of bytes in the file is returned.
errno, fstat(), lseek(), tell()
#include <sys/types.h> #include <fcntl.h> #include <stdio.h> #include <unistd.h> void main() { int filedes ; /* open a file for input */ filedes = open( "file", O_RDONLY ); if( filedes != -1 ) { printf( "Size of file is %ld bytes\n", filelength( filedes ) ); close( filedes ); } }
produces the output:
Size of file is 461 bytes
WATCOM
All (except DOS/PM)