Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Microsoft C 6.0 - <b>fstat() get information about open file</b> http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
 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     For a file, this is the drive number of the disk
                    containing the file; for a device, this is the
                    handle.

        st_rdev     Same as 'st_dev'.

       st_nlink     Always set to the constant 1.

        st_size     File size in bytes.

       st_atime     Time of the most recent modification of the file.

       st_mtime     Same as 'st_atime' and 'st_ctime'.

       st_ctime     Same as 'st_atime' and 'st_mtime'.

    Returns:    Zero if successful; on error, -1 is returned and 'errno'
                is set to EBADF (bad file handle).

      Notes:    Three additional fields in the 'stat' structure,
                ('st_ino', 'st_uid', 'st_gid'), contain 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.

 Portability:   Not supported by ANSI standard.

   -------------------------------- 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));
                    }
               }
           }



See Also: stat() access() chmod() filelength()

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson