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

XBi*_*13X 12 c unix file unix-timestamp

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

Fre*_*Foo 12

这将返回文件的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;
}
Run Code Online (Sandbox Code Playgroud)