Gets status information on a file.
#include <sys\types.h>
#include <sys\stat.h>
int _stat( char *pathname, struct _stat *buffer);
pathname | Path name of existing file | |
buffer | Pointer to structure that receives results |
The _stat function obtains information about the file or directory specified by pathname and stores it in the structure pointed to by buffer. The _stat structure, defined in the file SYS\STAT.H, includes the following fields:
Field | Value |
st_atime | Time of last access of file. |
st_ctime | Time of creation of file. |
st_dev | Drive number of the disk containing the file (same as st_rdev). Real mode only. |
st_mode | Bit mask for file-mode information. The _S_IFDIR bit is set if pathname specifies a directory; the _S_IFREG bit is set if pathname specifies an ordinary file. User read/write bits are set according to the file's permission mode; user execute bits are set according to the filename extension. |
st_mtime | Time of last modification of file. |
st_nlink | Always 1. |
st_rdev | Drive number of the disk containing the file (same as st_dev). Real mode only. |
st_size | Size of the file in bytes. |
Note that if pathname refers to a device, the size and time fields in the _stat structure are not meaningful. Also, as STAT.H uses the dev_t type, which is defined in TYPES.H, you must include TYPES.H before STAT.H in your code.
The _stat function returns 0 if the file-status information is obtained. A return value of –1 indicates an error; also, errno is set to ENOENT, indicating that the filename or path name could not be found.
Standards:UNIX
16-Bit:DOS, QWIN, WIN, WIN DLL
32-Bit:DOS32X
Use _stat for compatibility with ANSI naming conventions of non-ANSI functions. Use stat and link with OLDNAMES.LIB for UNIX compatibility.
/* STAT.C: This program uses the _stat function to report information
* about the file named STAT.C.
*/
#include <time.h>
#include <sys\types.h>
#include <sys\stat.h>
#include <stdio.h>
void main( void )
{
struct _stat buf;
int fh, result;
char buffer[] = "A line to output";
/* Get data associated with "stat.c": */
result = _stat( "stat.c", &buf );
/* Check if statistics are valid: */
if( result != 0 )
perror( "Problem getting information" );
else
{
/* Output some of the statistics: */
printf( "File size : %ld\n", buf.st_size );
printf( "Drive : %c:\n", buf.st_dev + 'A' );
printf( "Time modified : %s", ctime( &buf.st_atime ) );
}
}
File size : 761
Drive : C:
Time modified : Mon Jun 14 12:20:08 1999