c - 在C中获取文件的最后修改日期

标签 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 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/

相关文章:

c - 我在c中找不到几何平均值

不同硬币的硬币变化上限不同吗?

c - 如何解析来自MCU UART的不确定长度数据包?

mysql从UTC转换为IST

mysql - 无法使用 mySQL 将字符串格式化为时间戳?

java - 如何从文件中读取大的 clob 数据?

c - 结构帮助。将卡片结构分配给甲板结构中保存的卡片数组

database - 如何按日期列对 oracle 表进行分区?

c# - 系统.IO.IOException : file used by another process

file-io - 修改clojure中的Clojure源代码文件