Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Finding Timestamp of a File in C?

Status
Not open for further replies.

kelon

Programmer
May 7, 2002
4
CA
Hi,

I was just wondering how you would determine the date/timestamp of a file using C. Thanks.
 
Time stamps can be read by using the stat() function. For more information... try man stat.

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>

main()
{
int status = 0;
struct stat statData;

status = stat(&quot;MyFile.dat&quot;, &statData);

if(status == -1)
{
/*
*
* Some error occured, should use errno to check
* but for this example... just exit.
*
*/
fprintf(stderr, &quot;stat() failed.&quot;);
exit(1);
}

printf(&quot;Last Modification : %s&quot;,
ctime(&statData.st_mtime));
printf(&quot;Last Access : %s&quot;,
ctime(&statData.st_atime));
printf(&quot;Last Change Of File Status: %s&quot;,
ctime(&statData.st_ctime));

return 0;
}
 
Hi,

I tried the code and it works great! Thanks a lot!

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top