如何获得文件创建日期在Linux呢? [英] How to get file creation date in Linux?
问题描述
我与包含有关在其生命的不同时期同一个对象,并命令他们是创建日期的唯一途径信息的文件批工作。
我用的是这样的:
I am working with batches of files that contain information about the same object at the different times of its life, and the only way to order them is by creation date. I was using this:
//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;
但是,这并不似乎工作。
我究竟做错了什么?是否有其他更可靠的/简单的方法来获得Linux下的文件创建日期?
But that does not seems to work. What am I doing wrong? Are there other, more reliable/simple ways to get file creation date under Linux?
推荐答案
在文件描述符FSTAT的作品,而不是文件结构。最简单的版本:
fstat works on file descriptors, not FILE structures. The simplest version:
#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("%i\n", birthtime(st));
}
return 0;
}
您将需要通过检查SYS / stat.h或使用某种类型的autoconf构造搞清楚,如果你的系统有st_birthtime在其统计的结构。
You will need to figure out if your system has st_birthtime in its stat structure by inspecting sys/stat.h or using some kind of autoconf construct.
这篇关于如何获得文件创建日期在Linux呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!