Solved

Get last modified file time in C

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

Technology Partners: 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

In this article, I will show you HOW TO: Perform a Physical to Virtual (P2V) Conversion the easy way from a computer backup (image).
Determining the an SCCM package name from the Package ID
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

732 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