如何在linux上显示文件的最后修改时间

Jav*_*ile 11 c linux time file

我想写一个C程序来显示文件的最后修改时间,以微秒或毫秒为单位.我该怎么办?你能给我一个帮助吗?

非常感谢.

caf*_*caf 11

使用该stat()功能.在glibc的最新版本中,st_mtim(注意:没有尾随e)是一个struct timespec包含文件修改时间的类型字段:

struct stat st;

if (stat(filename, &st)) {
    perror(filename);
} else {
    printf("%s: mtime = %lld.%.9ld\n", filename, (long long)st.st_mtim.tv_sec, st.st_mtim.tv_nsec);
}
Run Code Online (Sandbox Code Playgroud)

您应该检查构建系统中是否存在st_mtimin struct stat,并且如果不存在,则准备好回退st_mtime(具有类型time_t,只有1秒分辨率).