我需要在C程序中更新現有文件的文件時間戳(即st_ctime
,st_atime
,st_mtime
)。如何在Linux中設置文件時間戳?
在Win32上,我可以使用SetFileTime
來更新時間戳。
如何在Linux上實現相同?
編輯
我知道touch
但因爲我已經把C
標籤的問題,我希望我能有一個系統調用做到這一點,而不是調用外部命令
我需要在C程序中更新現有文件的文件時間戳(即st_ctime
,st_atime
,st_mtime
)。如何在Linux中設置文件時間戳?
在Win32上,我可以使用SetFileTime
來更新時間戳。
如何在Linux上實現相同?
編輯
我知道touch
但因爲我已經把C
標籤的問題,我希望我能有一個系統調用做到這一點,而不是調用外部命令
我不知道,你可以更改文件的創建時間,但你可以使用utime功能更改訪問和修改時間。
'st_ctime'是狀態更改時間,而不是創建時間。實際上,就我所知,在Linux所使用的正常文件系統中,如ext3/ext4,文件系統不存儲文件創建時間。 –
您正在尋找utimensat
or futimens
:
int utimensat(int dirfd, const char *pathname,
const struct timespec times[2], int flags);
int futimens(int fd, const struct timespec times[2]);
utimensat,futimens - 更改文件的時間戳以納秒精度
類似'system(「touch」)''? –
@SouravGhosh:哦,C'Mon!我試圖寫嚴重的代碼,沒有黑客... – eckes