2015-05-07 44 views
0

我在linux上使用了stat()函数来检索有关文件的详细信息。
其中一个细节是上次访问时间存储在变量“st_atime
但是,什么是格式说明符来显示这个detail.My程序不断抛出错误。
打印“stat”缓冲区内容的格式说明符

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

int main() 
{ 
    struct stat buf; 
    stat("reversi.py",&buf); 
    printf("The size is...%d\n",buf.st_atime); 
    return 0; 
} 

的错误是

warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘__time_t’ [-Wformat=] 
    printf("The size is...%d\n",buf.st_atime); 

,这是什么类型的数据的正确格式说明。
此外还有几个更多的细节返回的功能。是否有一个地方,我可以找到所有这些细节正确的格式说明符。
谢谢。

+0

使用例如['strftime'](http://en.cppreference.com/w/c/chrono/strftime)将时间格式化为字符串。或[相关日期/时间功能](http://en.cppreference.com/w/c/chrono)之一。 –

+0

@JoachimPileborg它变得乏味,如果我必须转换所有details.Any shorcut? – Pradeep

+0

这就是它的工作原理,没有标准的'printf'格式来打印时间。 –

回答

0

的ctime()gmtime的()本地时间()功能的全部需要time_t的数据类型。

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

int main() 
{ 
    struct stat buf; 
    stat("1.c",&buf); 
    printf("Last Access was : %s\n",ctime(&buf.st_atime)); 
    return 0; 
} 

这将打印

Last Access was : Tue Apr 28 10:09:15 2015