如何在Linux中获取文件创建日期?
发布时间:2020-12-14 00:23:01 所属栏目:Linux 来源:网络整理
导读:我正在处理批量文件,这些文件在其生命的不同时间包含有关同一对象的信息,并且订购它们的唯一方法是创建日期. 我在用这个: //char* buffer has the name of filestruct stat buf;FILE *tf;tf = fopen(buffer,"r");//check handlefstat(tf,buf);fclose(tf);pM
我正在处理批量文件,这些文件在其生命的不同时间包含有关同一对象的信息,并且订购它们的唯一方法是创建日期.
我在用这个: //char* buffer has the name of file struct stat buf; FILE *tf; tf = fopen(buffer,"r"); //check handle fstat(tf,&buf); fclose(tf); pMyObj->lastchanged=buf.st_mtime; 但这似乎不起作用. 解决方法
fstat适用于文件描述符,而不适用于FILE结构.最简单的版本:
#include <sys/types.h> #include <sys/stat.h> #include <stdio.h> #ifdef HAVE_ST_BIRTHTIME #define birthtime(x) x.st_birthtime #else #define birthtime(x) x.st_ctime #endif int main(int argc,char *argv[]) { struct stat st; size_t i; for( i=1; i<argc; i++ ) { if( stat(argv[i],&st) != 0 ) perror(argv[i]); printf("%in",birthtime(st)); } return 0; } 您需要通过检查sys / stat.h或使用某种autoconf构造来确定您的系统是否在其stat结构中具有st_birthtime. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |