Get information about an open file.
int _fstat(
int fd,
struct _stat *buffer
);
int _fstat64(
int fd,
struct __stat64 *buffer
);
int _fstati64(
int fd,
struct _stati64 *buffer
);
Parameters
- fd
- File descriptor of open file.
- buffer
- Pointer to structure to store results.
Return Value
Return 0 if the file-status information is obtained. A return value of –1 indicates an error, in which case errno is set to EBADF, indicating an invalid file descriptor.
Remarks
The _fstat function obtains information about the open file associated with fd and stores it in the structure pointed to by buffer. The _stat structure, defined in SYS\STAT.H, contains the following fields:
- st_atime
- Time of last file access.
- st_ctime
- Time of creation of file.
- st_dev
- If a device, fd; otherwise 0.
- st_mode
- Bit mask for file-mode information. The _S_IFCHR bit is set if fd refers to a device. The _S_IFREG bit is set if fd refers to an ordinary file. The read/write bits are set according to the file's permission mode. _S_IFCHR and other constants are defined in SYS\STAT.H.
- st_mtime
- Time of last modification of file.
- st_nlink
- Always 1 on non-NTFS file systems.
- st_rdev
- If a device, fd; otherwise 0.
- st_size
- Size of the file in bytes.
If fd refers to a device, the st_atime, st_ctime, st_mtime, and st_size fields are not meaningful.
Because STAT.H uses the _dev_t type, which is defined in TYPES.H, you must include TYPES.H before STAT.H in your code.
_fstat64, which uses the __stat64 structure, allows file-creation dates to be expressed up through 23:59:59, December 31, 3000, UTC, whereas the other functions only represent dates through 19:14:07 January 18, 2038, UTC. Midnight, January 1, 1970, is the lower bound of the date range for all these functions.
Requirements
| Function | Required header | Compatibility |
| _fstat | <sys/stat.h> and <sys/types.h> | Win 98, Win Me, Win NT, Win 2000, Win XP |
| _fstat64 | <sys/stat.h> and <sys/types.h> | Win 98, Win Me, Win NT, Win 2000, Win XP |
| _fstati64 | <sys/stat.h> and <sys/types.h> | Win 98, Win Me, Win NT, Win 2000, Win XP |
For additional compatibility information, see Compatibility in the Introduction.
Libraries
All versions of the C run-time libraries.
Example
// crt_fstat.c
/* This program uses _fstat64 to report
* the size of a file named F_STAT.OUT.
*/
#include <io.h>
#include <fcntl.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main( void )
{
struct __stat64 buf;
int fd, result;
char buffer[] = "A line to output";
if( (fd = _open( "f_stat.out", _O_CREAT | _O_WRONLY |
_O_TRUNC )) == -1 )
_write( fd, buffer, strlen( buffer ) );
/* Get data associated with "fd": */
result = _fstat64( fd, &buf );
/* Check if statistics are valid: */
if( result != 0 )
printf( "Bad file file descriptor\n" );
else
{
printf( "File size : %ld\n", buf.st_size );
printf( "Time modified : %s", _ctime64( &buf.st_ctime ) );
}
_close( fd );
}
Sample Output
File size : 0
Time modified : Wed Feb 13 09:00:01 2002
See Also
File Handling Routines | _access | _chmod | _filelength | _stat | Run-Time Routines and .NET Framework Equivalents