title: Get last modified time of file in linux
body: <p>I am working on a C program where I need to get the last modified time of the file. What the program does is a function loops through each file within a directory and when a particular file(s) is found it calls another function to check that the last modified times of the file.</p>
  
  <p>Within the directory there is a <code>mylog.txt.1</code>, <code>mylog.txt.2</code> and <code>mylog.txt.3</code> etc. When I list the directory in linux using the ll command I can see that <code>mylog.txt.1</code> and <code>mylog.txt.2</code> were modified on the 4th May and <code>mylog.txt.3</code> was modified on the 3rd May. </p>
  
  <p>When the program checks each of these files however, it is always returning 3rd may. Below is the code that I am using. </p>
  
  <pre><code>void getFileCreationTime(char *filePath)
  {
      struct stat attrib;
      stat(filePath, &amp;attrib);
      char date[10];
      strftime(date, 10, "%d-%m-%y", gmtime(&amp;(attrib.st_ctime)));
      printf("The file %s was last modified at %s\n", filePath, date);
      date[0] = 0;
  }
  </code></pre>
  
  <p>I've tried all the different variations of <code>st_ctime</code>, i.e. <code>st_mtime</code> and <code>st_atime</code> but they all return 3rd may. </p>
  
  <p>Thanks for any help you can provide. </p>