Reading File Dates

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.

6 thoughts on “Reading File Dates

  1. This is completely irrelevant to your post (sorry!) but I wanted to say I have just bought your book on NCurses. I am writing an implementation of a Galton Board in C and needed to output characters at specific positions in the terminal rather than just in the next position. After a bit of searching I found a few suggestions that NCurses was the best way. I hadn’t heard of it until I saw the page here on your site so I thought I’d get the book and have a go with it.

    I have nearly finished the Galton Board without NCurses but might go back and re-write it in the future.

    Quantum Particle Bottling Co? Can you send me a pint of strange quarks please, thanks.

  2. Hey, thanks! I just got an email from a “30-year Usenix’er” yesterday who also has the book and even found some booboos on the webpage for me to fix. I think Ncurses is a blast. I’ve written a few games and some utilities. The full-screen approach reminds of programming a single-process system like DOS back in the old days.

    Quantum Particle Bottling only sells individual quantum particles. There is no guarantee that the particle survives shipping.

  3. If you keep another particle, due to entanglement you will be able to tell if the other has arrived safely. Or whether it’s turned into a dead cat. Or something.

    My first computer was a Sinclair ZX81 (apparently sold in the US as the Timex ZX81). Ncurses feels very ZX81ish. My dad bought it as he thought it might help him run his business. It didn’t, obviously!

    Do you have somewhere on this site specifically for posting Ncurses comments?

  4. I remember the Timex Z80 computer, which was pretty cool. It got a lot of people started in computing and programming.

    Eventually, I plan on doing an Ncurses series on the blog, but otherwise there’s no specific location for Ncurses questions or posts. The books have been quite popular to my surprise, so I suppose an Ncurses forum or something similar would be worthy.

  5. Maybe start a group on LinkedIn? I checked and there isn’t one already.

    Potential membership list:
    You
    Me
    err… that’s it really!

    I wrote a post the other day giving a very brief intro to Ncurses. I have written a couple of other posts recently which use basic terminal functionality for very rudimentary “graphics” but will write alternative front ends for them in Ncurses in due course.

  6. That sounds like a good idea. We might find more C programmers we didn’t know existed!

    Send me an invite on LinkedIn. You are one of many Chris Webbs, but I’m only one of a few Dan Gookins. (Yes, I’ve been cloned.)

Leave a Reply