obtain information about an open file
#include <sys/types.h> #include <sys/stat.h> int fstat( int filedes, struct stat *buf );
The fstat() function obtains information about an open file whose file descriptor is filedes. This information is placed in the structure located at the address indicated by buf.
The file sys/stat.h contains definitions for the structure stat.
At least the following macros are defined in the sys/stat.h header file:
The value m supplied to the macros is the value of the st_mode field of a stat structure. The macro evaluates to a non-zero value if the test is true, and zero if the test is false.
The access permissions for the file or directory are specified as a combination of bits in the st_mode field of a stat structure. These bits are defined in the sys/stat.h header file, and are described in the section on this file in “Header Files in /usr/include/sys” of the C Library Overview chapter. The following bits are also encoded in the st_mode field:
creat(), dup(), dup2(), errno, fcntl(), fsys_stat(), fsys_fstat(), lstat(), open(), sopen(), stat(), pipe()
#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> void main() { int filedes, rc; struct stat buf; filedes = open( "file", O_RDONLY ); if( filedes != -1 ) { rc = fstat( filedes , &buf ); if( rc != -1 ) printf( "File size = %d\n", buf.st_size ); close( filedes ); } }