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>stat() get file-status information on named file</b> http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
 stat()                  Get File-Status Information on Named File

 #include   <sys\types.h>
 #include   <sys\stat.h>

 int          stat(pathname,buffer);
 char         *pathname;                 Path name of existing file
 struct stat  *buffer;                   Pointer to struct receiving results

    stat() gets and stores information about a file or directory
    identified by 'pathname'.  The information, stored in the 'stat'
    structure, is pointed to by 'buffer'.  The 'stat' structure is
    defined in <sys\stat.h> and has the following fields:

            st_mode     Bit mask giving information about the
                        open file's mode.  S_IFDIR bit is set if 'pathname'
                        refers to a directory.  S_IFREG is set if 'pathname'
                        refers to an ordinary file.  Read/write permission
                        bits set according to the file's permission mode;
                        execute bits set using the file-name extension.

            st_dev      (MS-DOS or OS/2 real mode only) Drive number of the
                        disk on which the file is stored.

            st_rdev     (MS-DOS or OS/2 real mode only) 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'.
            st_ctime    Same as 'st_atime' and 'st_mtime'.

    Returns:    Zero if the file-status information has been successfully
                retrieved. On error, -1 is returned and 'errno' is set to
                ENOENT, indicating the file name or path name could not
                be found.

      Notes:    The size and time fields of 'stat' are not meaningful if
                'pathname' refers to a device.

                Three additional fields in the 'stat' structure hold
                values that are not meaningful under MS-DOS.

 Portability:   Does not support ANSI standard.

   -------------------------------- Example ---------------------------------
The following statements get the size of a file and the last time it was accessed.

           #include <fcntl.h>
           #include <sys\types.h>
           #include <sys\stat.h>
           #include <stdio.h>                /* for printf */
           #include <time.h>

           struct stat buffer;
           int status;
           FILE *stream;
           char ch;
           time_t *acctime;
           char *fname = "input.dat";

           main()
           {
               if((stream = fopen(fname,"r+")) != NULL) {
                     printf("input.dat opened\n");
                     if((status = stat(fname,&buffer)) == 0)  {
                        printf("file status obtained.\n");
                        printf("file size is %ld\n",buffer.st_size);
                        acctime = ctime(&buffer.st_atime);
                        printf("file last accessed: %s\n",acctime);
                     }
                    else {
                       printf("status != 0");
                       fclose(stream);
                   }
               }
           }

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

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