Solved

Get last modified file time in C

Posted on 2011-09-11
4
300 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
  • 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
If you get continual lockouts after changing your Active Directory password, there are several possible reasons.  Two of the most common are using other devices to access your email and stored passwords in the credential manager of windows.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now