_fstat

Description

Gets information about an open file.

#include <sys\types.h>

#include <sys\stat.h>

int _fstat( int handle, struct _stat *buffer );

handle Handle of open file  
buffer Pointer to structure to store results  

Remarks

The _fstat function obtains information about the open file associated with handle and stores it in the structure pointed to by buffer. The structure, whose type _stat is defined in SYS\STAT.H, contains the following fields:

Field Value

st_atime Time of last access of file.
st_ctime Time of creation of file.
st_dev Either the drive number of the disk containing the file, or handle in the case of a device (same as st_rdev).
st_mode Bit mask for file-mode information. The _S_IFCHR bit is set if handle refers to a device. The _S_IFREG bit is set if handle 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.
st_rdev Either the drive number of the disk containing the file, or handle in the case of a device (same as st_dev).
st_size Size of the file in bytes.

If handle refers to a device, the size and time fields in the _stat structure are not meaningful.

Return Value

The _fstat function returns the value 0 if the file-status information is obtained. A return value of –1 indicates an error; in this case, errno is set to EBADF, indicating an invalid file handle.

Compatibility

Standards:UNIX

16-Bit:DOS, QWIN, WIN, WIN DLL

32-Bit:DOS32X

Use _fstat for compatibility with ANSI naming conventions of non-ANSI functions. Use fstat and link with OLDNAMES.LIB for UNIX compatibility.

See Also

_access, _chmod, _filelength, _stat

Example

/* FSTAT.C: This program uses _fstat to report the size of a file

* named FSTAT.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>

void main( void )

{

struct _stat buf;

int fh, result;

char buffer[] = "A line to output";

if( (fh = _open( "f_stat.out", _O_CREAT | _O_WRONLY | _O_TRUNC )) == -1 )

exit( 1 );

_write( fh, buffer, strlen( buffer ) );

/* Get data associated with "fh": */

result = _fstat( fh, &buf );

/* Check if statistics are valid: */

if( result != 0 )

printf( "Bad file handle\n" );

else

{

printf( "File size : %ld\n", buf.st_size );

printf( "Drive number : %d\n", buf.st_dev );

printf( "Time modified : %s", ctime( &buf.st_atime ) );

}

_close( fh );

}

Output

File size : 16

Drive number : 0

Time modified : Tue Jun 15 21:38:46 1999