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

时间:2021-10-29 18:09:39

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?

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

2 个解决方案

#1


12  

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).

这将返回文件的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;
}

#2


2  

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

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

#1


12  

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).

这将返回文件的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;
}

#2


2  

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

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