gpt4 book ai didi

c - 如何在 OS X/C 中获取文件的最后修改时间?

转载 作者:行者123 更新时间:2023-11-30 20:36:24 25 4
gpt4 key购买 nike

我有以下代码:

struct stat info;
stat("/Users/john/test.txt", &info);
printf("%s\n", ctime(&info.st_mtimespec));

其中我试图获取文件的最后修改时间,如 ls -l 命令中显示的格式:

Jan 29 19:39

不幸的是,上面的代码不起作用。我在 xcode 上收到以下错误消息:

Conflicting types for ctime

我该如何修复它?如果有任何其他方法可以获取上述格式的时间,请务必提及。

最佳答案

检查 struct stat的声明中,您会看到 st_mtimespec 必须是 st_mtime

然后,根据这个question ,我像这样重新排列了你的代码:

struct stat info;
struct tm* tm_info;
char buffer[16];

stat("/Users/john/test.txt", &info);
tm_info = localtime(&info.st_mtime);
strftime(buffer, sizeof(buffer), "%b %d %H:%M", tm_info);

printf("%s\n", buffer);

希望有帮助。

关于c - 如何在 OS X/C 中获取文件的最后修改时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36213888/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com