2015-05-07 39 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