Syntax
#include <sys\stat.h> #include <sys\types.h> int fstat(int handle, struct stat *buffer);Description
fstat obtains information about the open file associated with the given handle and stores it in the structure to which buffer points. The <sys\stat.h> include file defines the stat structure. The stat structure contains the following fields:
Field
st_dev
There are three additional fields in the stat structure for portability to other operating systems; they have no meaning under the OS/2 operating system.
Note: If the given handle refers to a device, the size and time fields in the stat structure are not meaningful.
If it obtains the file status information, fstat returns 0. A return value of -1 indicates an error, and fstat sets errno to EBADF, showing an incorrect file handle.
This example uses fstat to report the size of a file named data.
#include <time.h>#include <sys\types.h>
#include <sys\stat.h>
#include <stdio.h>
int main(void)
{
   struct stat buf;
   FILE *fp;
   int fh,result;
   char *buffer = "A line to output";
   fp = fopen("data", "w+");
   fprintf(fp, "%s", buffer);
   fflush(fp);
   fclose(fp);
   fp = fopen("data", "r");
   if (0 == fstat(fileno(fp), &buf)) {
      printf("file size is %ld\n", buf.st_size);
      printf("time modified is %s\n", ctime(&buf.st_atime));
   }
   else
      printf("Bad file handle\n");
   fclose(fp);
   return 0;
   /****************************************************************************
      The output should be similar to:
      file size is 16
      time modified is Thu May 16 16:08:14 1995
   ****************************************************************************/
}
Related Information