Reading File Dates - JohnHau/mis GitHub Wiki

The process of opening, reading, writing, and closing files is basic C language stuff. For specific details about a file, the stat() function is used to fill a stat structure. Unfolded are the nitty-gritties about the file itself, such as its size, ownership, and up to four time-date stamps.

I explore the stat structure in my book, Beginning Programming with C For Dummies. Recently, I wrote a utility that compares the ages of files in a directory. To do that, the code fetched the date members of the stat structure related to those files. Up to four members are available:

st_atime returns the date/time when the file was last accessed, which includes when the file was opened and read but not changed.

st_mtime returns the date/time when the file was last modified, updated, or changed by the system.

st_ctime returns the date/time the file’s status was last changed by a user.

st_birthtime returns the date/time the file was created. This field may not be available on all operating systems or implemented by all C language libraries.

For my utility, I wanted to check the st_birthtime member. To ensure that it works, I checked the file’s date as reported in the directory listing, but also examined the other three stat structure members as well. Here’s a test program I wrote:

#include <stdio.h> #include <sys/stat.h> #include <time.h>

int main() { struct stat filestat;

stat("gettysburg.txt",&filestat);
/* newline included in ctime() output */
printf(" File access time %s",
        ctime(&filestat.st_atime)
      );
printf(" File modify time %s",
        ctime(&filestat.st_mtime)
      );
printf("File changed time %s",
        ctime(&filestat.st_ctime)
      );
printf("  File birth time %s",
        ctime(&filestat.st_birthtime)
      );

return(0);

} The file examined is gettysburg.txt, cited at Line 9. It’s a text file containing Lincoln’s Gettysburg Address. If you’re compiling this code on your system, ensure that you name a file available in the same directory in which you run the program. Click here to view/download the gettysburg.txt file.

The stat() function’s return value isn’t checked in this code because I know that gettysburg.txt exists. In code you plan on releasing to the wild, check the return value: -1 indicates some type of file error.

Upon success, the stat() function fills the structure filestat (Line 7) with details about the file. The ampersand & is used because stat() requires a pointer (memory address) as its second argument.

Once filled, filestat structure members are used in subsequent printf() statements. The ctime() function converts each timestamp value to a date-time string. The time value is stored in Unix epoch time.

Here’s sample output:

File access time Fri Feb 9 19:44:36 2018 File modify time Sat Sep 5 08:59:18 2015 File changed time Wed Sep 9 17:02:24 2015 File birth time Sat Sep 5 08:59:18 2015 I last viewed the file on February 9th, earlier this year. The file was created and last modified on September 5, 2015. It was changed on September 9 2015.

This code didn’t compile with my version of Code::Blocks, MinGW 4.7.1 because the st_birthtime structure member isn’t defined. Newer versions of MinGW might compile the code. And if the st_birthtime isn’t available from the operating system, the value zero is returned. The ctime() function interprets a timestamp of zero as Wed Dec 31 16:00:00 1969.

⚠️ **GitHub.com Fallback** ⚠️