fstat() Get Information about Open File
#include <sys\types.h>
#include <sys\stat.h>
int fstat(handle,buffer);
int handle; Handle referring to open file
struct stat *buffer; Pointer to results (a structure)
fstat() gets and stores information about the open file associated
with 'handle'. The information is stored in the structure pointed to
by 'buffer'. The 'stat' structure (defined in <sys\stat.h>) has the
following fields:
st_mode Bit mask giving information about the file's mode.
S_IFCHR bit is set if 'handle' refers to a device.
S_IFREG is set if 'handle' refers to an ordinary
file. One or both of S_IREAD and S_IWRITE will be
set according to the file's permission mode.
st_dev Drive number of the disk containing the file, or file
handle if the file refers to a device.
st_rdev Same as 'st_dev'.
st_nlink Always set to the constant 1.
st_size Size of the file in bytes.
st_atime Time of the most recent modification of the file.
st_mtime Same as 'st_atime'.
st_ctime Same as 'st_atime' and 'st_mtime'.
Returns: Zero if the file-status information has been successfully
retrieved. A value of -1 indicates a failure to get the
information. On errer, 'errno' is set to EBADF
indicating a bad file handle.
Notes: The 'stat' structure contains three additional fields
that hold values that are not meaningful under MS-DOS.
'st_size' and 'st_atime' (and therefore 'st_mtime' and
'st_ctime') contain meaningless information if 'handle'
refers to a device.
-------------------------------- Example ---------------------------------
The following statements open a preexisting file, and, if file status
is obtained, print out the file size and last time accessed.
#include <fcntl.h>
#include <sys\types.h>
#include <sys\stat.h>
#include <stdio.h>
#include <time.h>
struct stat buffer;
int status;
FILE *stream;
char ch;
struct tm *acctime;
main()
{
if((stream = fopen("input.dat","r+")) != NULL) {
if((status = fstat(fileno(stream),&buffer)) == 0) {
printf("file status obtained.\n");
printf("file size is %ld\n",buffer.st_size);
acctime = gmtime(((long)&buffer.st_atime);
printf("file last accessed: %s\n",asctime(acctime));
}
}
}
Seealso:
This page last updated on Fri Nov 30 10:48:32 MSK 2001
Copyright © 1992-2001, Vitaly Filatov, Moscow, Russia
Webmaster