在 C 中使用 utime 获取 UNIX 上的文件修改时间

发布于 2024-09-29 06:05:10 字数 130 浏览 2 评论 0原文

一位教授告诉我,您可以使用utime.h获取文件的最后修改时间。然而,手册页似乎引用了 utime() 仅设置此值。如何在 UNIX 系统上的 C 语言中查找文件上次更改的时间?

I have been told by a professor that you can get a file's last modification time by using utime.h. However, the man page seem to cite that utime() only sets this value. How can I look up the last time a file was changed in C on a UNIX system?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

衣神在巴黎 2024-10-06 06:05:10

这将返回文件的mtime,即“上次数据修改的时间”。请注意,Unix 也有一个概念ctime ,“上次状态更改的时间”(另请参阅 ctime、atime、mtime )。

#include <sys/types.h>
#include <sys/stat.h>

time_t get_mtime(const char *path)
{
    struct stat statbuf;
    if (stat(path, &statbuf) == -1) {
        perror(path);
        exit(1);
    }
    return statbuf.st_mtime;
}

This returns the file's mtime, the "time of last data modification". Note that Unix also has a concept ctime, the "time of last status change" (see also ctime, atime, mtime).

#include <sys/types.h>
#include <sys/stat.h>

time_t get_mtime(const char *path)
{
    struct stat statbuf;
    if (stat(path, &statbuf) == -1) {
        perror(path);
        exit(1);
    }
    return statbuf.st_mtime;
}
半岛未凉 2024-10-06 06:05:10

您可以使用 stat 系统调用来获取最后访问和修改时间。

You can use the stat system call to get the last access and modification times.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文