Header
sys/stat.h
sys/types.h
Prototype
int _stat( char *path, struct _stat *buf);
Description
The _stat function gets information about a file or directory specified by path and store the information in the structure that buf points to. The structure contains the following fields:
Field/Description
- st_dev
- The drive number of the drive specified in path, or the default drive, if none is given.
- st_mode
- Bit map containing mode information on the open file, made up of the values below.
- st_nlink
- Always 1.
- st_rdev
- Same as st_dev.
- st_size
- Size of file in bytes.
- st_mtime
- Time last modified.
- st_atime
- For NT, time last accessed; or same as st_mtime.
- st_ctime
- For NT, time created; otherwise same as st_mtime.
- st_ino
- Always 0. (stat structure only)
- st_uid
- Always 0. (stat structure only)
- st_gid
- Always 0. (stat structure only)
Use the following values for st_mode:
- _S_IFREG
- Set if path refers to an ordinary file, not a directory.
- _S_IREAD
- Set if path refers to a readable file or directory.
- _S_IWRITE
- Set if path refers to a writable file or directory.
- _S_IFDIR
- Set if path refers to a directory.
- _S_IEXEC
- Set if path refers to an executable file or a directory.
Synonym
Function: stat
Values: S_IFREG, S_IREAD, S_IWRITE, S_IFDIR, S_IEXEC
Return Value
stat returns 0 if the status information is retrieved. On error the function returns -1 and errno is set.
Compatibility
DOS Windows 3.x Phar Lap DOSX Win32
See Also
_fstat
Example
/* Example of _stat */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <sys\stat.h>
void main ()
{
char filen[_MAX_PATH];
char *date;
int res;
struct _stat fstat;
printf (" Enter a filename: ");
gets (filen);
if (( res = _stat (filen, &fstat)) != 0)
{
perror (" Failure calling _stat");
exit (EXIT_FAILURE);
}
date = asctime (localtime (& fstat. st_ctime));
printf ("\ nDate: %s", date);
printf (" Mode: %d\ n", fstat. st_mode);
printf (" Size: %ld\ n", fstat. st_size);
}
Output
Enter a filename: _stat.c
Date: Thu Jun 23 09: 42: 04 1994
Mode: -32330
Size: 585