Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Borland C++ 2.x ( with Turbo C ) - <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\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 the file or directory
    specified by 'pathname'.  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 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     Drive number of the disk containing the file.

        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:     0, if successful. A value of -1 indicates a failure
                    to get the information.  On error, 'errno' is set to
                    ENOENT, indicating the file name or path name could
                    not be found.

         Notes:     If 'pathname' refers to a device, the size and time
                    fields of the 'stat' structure are not meaningful,
                    and 'st_dev' contains the file handle.

                    The 'stat' structure contains three additional fields
                    to those named above, but these hold values that are
                    not meaningful under MS-DOS.

   -------------------------------- Example ---------------------------------


    The following statements open and existing file and, if status is
    obtained, print out the file size and the last time it was accessed.

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

           struct stat buffr;
           int status;
           FILE *stream;
           char ch;
           struct tm *acctime;

           main()
           {
              if((stream = fopen("input.dat","r+")) != NULL) {
                 if((status = stat("input.dat",&buffr)) == 0)  {
                    printf("file status obtained.\n");
                    printf("file size is %ld bytes\n",buffr.st_size);
                    acctime = gmtime(&buffr.st_atime);
                    printf("file last accessed: %s\n",asctime(acctime));
                    }
              }
           }



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

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