我想用 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_mtimespec
。然而,当使用这个时,我的 Eclipse 告诉我 passing argument 1 of 'gmtime' from incompatible pointer type
on the line clock = gmtime(&(attr.st_mtimespec));
我做错了什么?
PS:我正在 OSX Snow Leopard、Eclipse CDT 上进行开发,并使用 GCC 作为跨平台编译器
最佳答案
在 OS X 上,st_mtimespec.tv_sec
等同于 st_mtime
。
为了使这个可移植,做
#ifdef __APPLE__
#ifndef st_mtime
#define st_mtime st_mtimespec.tv_sec
#endif
#endif
然后使用st_mtime
。
关于c - 在C中获取文件的最后修改日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11373505/