获取C中文件的上次修改日期

时间:2012-07-07 08:29:02

标签: 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_mtimespec。然而,当使用这个时,我的Eclipse告诉我passing argument 1 of 'gmtime' from incompatible pointer type

clock = gmtime(&(attr.st_mtimespec));

我做错了什么?

PS:我正在开发OSX Snow Leopard,Eclipse CDT并使用GCC作为跨平台编译器

1 个答案:

答案 0 :(得分:5)

在OS X上,st_mtimespec.tv_sec相当于st_mtime

要使其便携,请执行

#ifdef __APPLE__
#ifndef st_mtime
#define st_mtime st_mtimespec.tv_sec
#endif
#endif

然后使用st_mtime

相关问题