Solved

Get last modified file time in C

Posted on 2011-09-11
4
309 Views
Last Modified: 2012-05-12
Hey all,

  I'm looking to get the last time that a file was modified in C using the Windows API (or not..whichever is easiest).

  I need the time in an integer epoch timestamp (unix style timestamp).  Is this possible?

-Kevin
0
Comment
Question by:ProjectZIG
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 8

Expert Comment

by:ssnkumar
ID: 36520956
Use GetFileTime() API to get the "Created Time", "Last Accessed Time" and "Last Modified time".
Look at the following url for more details on this API: http://msdn.microsoft.com/en-us/library/ms724320

The o/p will be in the FILETIME structure.

You can use FileTimeToSystemTime() API to convert from the FILETIME structure to System Time.
This url gives the details of the API: http://msdn.microsoft.com/en-us/library/ms724280

Look at the following example showing how to use these API's: http://msdn.microsoft.com/en-us/library/ms724926

Here is a list of all the time related windows API's: http://msdn.microsoft.com/en-us/library/ms725473
0
 
LVL 7

Accepted Solution

by:
tampnic earned 250 total points
ID: 36522243
ssnkumar gives you all the information required to fill a SYSTEMTIME structure with your last modified date.

To convert the Windows SYSTEMTIME into a unix time you will need to copy the SYSTEMTIME values into a tm struct, then use the mktime() function to get the integer epoch timestamp. I have copied the definitions of "SYSTEMTIME" and "tm" into the code snippet below so you can see how the elements of each struct map to each other. Don't worry about filling the tm_wday, tm_yday and  tm_isdst  flags of your tm struct, mktime() should work OK without them.

http://msdn.microsoft.com/en-us/library/d1y53h2a(v=vs.80).aspx explains use of the mktime function.

Cheers,
   Chris

 
#include <time.h> // use the POSIX time functions

typedef struct _SYSTEMTIME {
  WORD wYear;
  WORD wMonth;
  WORD wDayOfWeek;
  WORD wDay;
  WORD wHour;
  WORD wMinute;
  WORD wSecond;
  WORD wMilliseconds;
} SYSTEMTIME, *PSYSTEMTIME;

struct tm {
        int tm_sec;     /* seconds after the minute - [0,59] */
        int tm_min;     /* minutes after the hour - [0,59] */
        int tm_hour;    /* hours since midnight - [0,23] */
        int tm_mday;    /* day of the month - [1,31] */
        int tm_mon;     /* months since January - [0,11] */
        int tm_year;    /* years since 1900 */
        int tm_wday;    /* days since Sunday - [0,6] */
        int tm_yday;    /* days since January 1 - [0,365] */
        int tm_isdst;   /* daylight savings time flag */
        };

Open in new window

0
 
LVL 2

Expert Comment

by:xeltek
ID: 36526745
Hi,
I had also used standard c library to do this.
If you have interest, you can find the code below here.
http://msdn.microsoft.com/en-us/library/14h5k7ff.aspx

// crt_stat.c
// This program uses the _stat function to
// report information about the file named crt_stat.c.
 
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <errno.h>

int main( void )
{
   struct _stat buf;
   int result;
   char timebuf[26];
   char* filename = "crt_stat.c";
   errno_t err;

   // Get data associated with "crt_stat.c": 
   result = _stat( filename, &buf );

   // Check if statistics are valid: 
   if( result != 0 )
   {
      perror( "Problem getting information" );
      switch (errno)
      {
         case ENOENT:
           printf("File %s not found.\n", filename);
           break;
         case EINVAL:
           printf("Invalid parameter to _stat.\n");
           break;
         default:
           /* Should never be reached. */
           printf("Unexpected error in _stat.\n");
      }
   }
   else
   {
      // Output some of the statistics: 
      printf( "File size     : %ld\n", buf.st_size );
      printf( "Drive         : %c:\n", buf.st_dev + 'A' );
      err = ctime_s(timebuf, 26, &buf.st_mtime);
      if (err)
      {
         printf("Invalid arguments to ctime_s.");
         exit(1);
      }
      printf( "Time modified : %s", timebuf );
   }
}

Open in new window

0
 
LVL 8

Expert Comment

by:ssnkumar
ID: 36914881
I don't know why you didn't find my answer helpful!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A small collection of useful tips and tricks for Windows 10 users that I decided to write as a result of recent questions that were asked and answered at Experts Exchange. Two short video tutorials included. Enjoy..
This article provides a convenient collection of links to Microsoft provided Security Patches for operating systems that have reached their End of Life support cycle. Included operating systems covered by this article are Windows XP,  Windows Server…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question