Solved

Get last modified file time in C

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Audit active directory trust relationships 2 27
Pay to 支付宝 12 56
Reset AD password via a webpage - Mac environment 6 41
2008 R2 export VM includes VHD 1 6
When you start your Windows 10 PC and got an "Operating system not found" error or just saw  "Auto repair for startup" or a blinking cursor with black screen. A loop for Auto repair will start but fix nothing.  You will be panic as there are no back…
Possible fixes for Windows 7 and Windows Server 2008 updating problem. Solutions mentioned are from Microsoft themselves. I started a case with them from our Microsoft Silver Partner option to open a case and get direct support from Microsoft. If s…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

860 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