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

F.P*_*F.P 8 c file-io compiler-errors date

我想在C中获取文件的最后修改日期.几乎所有我发现的源都使用了这个片段:

char *get_last_modified(char *file) {
    struct tm *clock;
    struct stat attr;

    stat(file, &attr);
    clock = gmtime(&(attr.st_mtime));

    return asctime(clock);
}
Run Code Online (Sandbox Code Playgroud)

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作为跨平台编译器

mpa*_*tel 6

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

为了使这个便携,做

#ifdef __APPLE__
#ifndef st_mtime
#define st_mtime st_mtimespec.tv_sec
#endif
#endif
Run Code Online (Sandbox Code Playgroud)

然后使用st_mtime.