Issue
Program:
#include<stdio.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<unistd.h>
void main()
{
struct stat stbuf;
stat("alphabet",&stbuf);
printf("Access time = %d\n",stbuf.st_atime);
printf("Modification time = %d\n",stbuf.st_mtime);
printf("Change time = %d\n",stbuf.st_mtime);
}
The above program gives the following output:
Output:
$ ./a.out
Access time = 1441619019
Modification time = 1441618853
Change time = 1441618853
$
It print the date in seconds. In C, what is the way to print the time as human readable format which is returned by stat function. Return type of stbuf.st_atime is __time_t.
Thanks in Advance...
Solution
Try to use char* ctime (const time_t * timer);
function from time.h library.
#include <time.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
int main(void)
{
struct stat stbuf;
stat("alphabet", &stbuf);
printf("Access time = %s\n", ctime(&stbuf.st_atime));
printf("Modification time = %s\n", ctime(&stbuf.st_mtime));
printf("Change time = %s\n", ctime(&stbuf.st_mtime));
}
It will give your following result:
$ ./test
Access time = Mon Sep 07 15:23:31 2015
Modification time = Mon Sep 07 15:23:31 2015
Change time = Mon Sep 07 15:23:31 2015
Answered By - ales-by Answer Checked By - Mary Flores (WPSolving Volunteer)