?
Solved

Get last modified file time in C

Posted on 2011-09-11
4
Medium Priority
?
318 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 1000 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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

Windows 10 Creator Update has just been released and I have it working very well on my laptop. Read below for issues, fixes and ideas.
Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

771 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