{"id":3004,"date":"2018-03-24T00:01:45","date_gmt":"2018-03-24T07:01:45","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=3004"},"modified":"2018-03-17T14:42:05","modified_gmt":"2018-03-17T21:42:05","slug":"reading-file-dates","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3004","title":{"rendered":"Reading File Dates"},"content":{"rendered":"<p>The process of opening, reading, writing, and closing files is basic C language stuff. For specific details about a file, the <em>stat()<\/em> function is used to fill a <code>stat<\/code> structure. Unfolded are the nitty-gritties about the file itself, such as its size, ownership, and up to four time-date stamps.<br \/>\n<!--more--><br \/>\nI explore the <code>stat<\/code> structure in my book, <em>Beginning Programming with C For Dummies<\/em>. 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 <code>stat<\/code> structure related to those files. Up to four members are available:<\/p>\n<p><code>st_atime<\/code> returns the date\/time when the file was last accessed, which includes when the file was opened and read but not changed.<\/p>\n<p><code>st_mtime<\/code> returns the date\/time when the file was last modified, updated, or changed by the system.<\/p>\n<p><code>st_ctime<\/code> returns the date\/time the file&#8217;s status was last changed by a user.<\/p>\n<p><code>st_birthtime<\/code> 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.<\/p>\n<p>For my utility, I wanted to check the <code>st_birthtime<\/code> member. To ensure that it works, I checked the file&#8217;s date as reported in the directory listing, but also examined the other three <code>stat<\/code> structure members as well. Here&#8217;s a test program I wrote:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;sys\/stat.h&gt;\r\n#include &lt;time.h&gt;\r\n\r\nint main()\r\n{\r\n    struct stat filestat;\r\n\r\n    stat(\"gettysburg.txt\",&amp;filestat);\r\n    <span class=\"comments\">\/* newline included in ctime() output *\/<\/span>\r\n    printf(\" File access time %s\",\r\n            ctime(&amp;filestat.st_atime)\r\n          );\r\n    printf(\" File modify time %s\",\r\n            ctime(&amp;filestat.st_mtime)\r\n          );\r\n    printf(\"File changed time %s\",\r\n            ctime(&amp;filestat.st_ctime)\r\n          );\r\n    printf(\"  File birth time %s\",\r\n            ctime(&amp;filestat.st_birthtime)\r\n          );\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The file examined is <code>gettysburg.txt<\/code>, cited at Line 9. It&#8217;s a text file containing Lincoln&#8217;s Gettysburg Address. If you&#8217;re compiling this code on your system, ensure that you name a file available in the same directory in which you run the program. <a href=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2015\/09\/gettysburg.txt\">Click here<\/a> to view\/download the <code>gettysburg.txt<\/code> file.<\/p>\n<p>The <em>stat()<\/em> function&#8217;s return value isn&#8217;t checked in this code because I know that <code>gettysburg.txt<\/code> exists. In code you plan on releasing to the wild, check the return value: -1 indicates some type of file error.<\/p>\n<p>Upon success, the <em>stat()<\/em> function fills the structure <code>filestat<\/code> (Line 7) with details about the file. The ampersand <code>&amp;<\/code> is used because <em>stat()<\/em> requires a pointer (memory address) as its second argument.<\/p>\n<p>Once filled, <code>filestat<\/code> structure members are used in subsequent <em>printf()<\/em> statements. The <em>ctime()<\/em> function converts each timestamp value to a date-time string. The time value is stored in Unix epoch time.<\/p>\n<p>Here&#8217;s sample output:<\/p>\n<pre><code> File access time Fri Feb  9 19:44:36 2018\r\n File modify time Sat Sep  5 08:59:18 2015\r\nFile changed time Wed Sep  9 17:02:24 2015\r\n  File birth time Sat Sep  5 08:59:18 2015<\/code><\/pre>\n<p>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.<\/p>\n<p>This code didn&#8217;t compile with my version of Code::Blocks, MinGW 4.7.1 because the <code>st_birthtime<\/code> structure member isn&#8217;t defined. Newer versions of MinGW might compile the code. And if the <code>st_birthtime<\/code> isn&#8217;t available from the operating system, the value zero is returned. The <em>ctime()<\/em> function interprets a timestamp of zero as <code>Wed Dec 31 16:00:00 1969<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You may see only one date in the directory, but a file has up to four date stamps. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3004\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-3004","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3004","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3004"}],"version-history":[{"count":8,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3004\/revisions"}],"predecessor-version":[{"id":3028,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3004\/revisions\/3028"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3004"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3004"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3004"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}