开发者

Getting file modification time on UNIX using utime in C

开发者 https://www.devze.com 2023-01-22 12:43 出处:网络
I have been told by a professor that you can get a file\'s last modification time by using 开发者_JS百科utime.h. However, the man page seem to cite that utime() only sets this value. How can I look up

I have been told by a professor that you can get a file's last modification time by using 开发者_JS百科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?


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;
}


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

0

精彩评论

暂无评论...
验证码 换一张
取 消