获取C中文件的最后修改日期
我想用C语言获取文件的最后修改日期。我找到的几乎所有源代码都使用了以下代码段:获取C中文件的最后修改日期,c,date,file-io,compiler-errors,C,Date,File Io,Compiler Errors,我想用C语言获取文件的最后修改日期。我找到的几乎所有源代码都使用了以下代码段: char *get_last_modified(char *file) { struct tm *clock; struct stat attr; stat(file, &attr); clock = gmtime(&(attr.st_mtime)); return asctime(clock); } 但是attr甚至没有字段st_mtime,只有st_m
char *get_last_modified(char *file) {
struct tm *clock;
struct stat attr;
stat(file, &attr);
clock = gmtime(&(attr.st_mtime));
return asctime(clock);
}
但是attr
甚至没有字段st_mtime
,只有st_mtimespec
。然而,当使用这个函数时,我的Eclipse告诉我,在clock=gmtime(&(attr.st_mtimespec))行上从不兼容的指针类型传递'gmtime'的参数1代码>
我做错了什么
PS:我正在OSX雪豹、EclipseCDT上开发,并在OSX上使用GCC作为跨平台编译器,st_mtimespec.tv_sec
相当于st_mtime
要使此便携,请执行以下操作:
#ifdef __APPLE__
#ifndef st_mtime
#define st_mtime st_mtimespec.tv_sec
#endif
#endif
然后使用st\u mtime
是,否则stat
本身将不可用。虽然OT:您不想存储get\u last\u modified()
返回的引用,不是吗asctime()
返回对静态内存的引用,在每次连续调用静态内存时,静态内存的内容都会被覆盖。它只被调用一次,但我会记住,如果这种情况发生变化:)有没有办法使这个交叉编译器?该程序必须在Ubuntu上运行,没有任何警告和错误,但我没有一个虚拟机上的程序。。。