在linux中获取文件的最后修改时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10446526/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get last modified time of file in linux
提问by Boardy
I am working on a C program where I need to get the last modified time of the file. What the program does is a function loops through each file within a directory and when a particular file(s) is found it calls another function to check that the last modified times of the file.
我正在开发一个 C 程序,我需要在其中获取文件的最后修改时间。该程序所做的是一个函数循环遍历目录中的每个文件,当找到特定文件时,它会调用另一个函数来检查文件的最后修改时间。
Within the directory there is a mylog.txt.1, mylog.txt.2and mylog.txt.3etc. When I list the directory in linux using the ll command I can see that mylog.txt.1and mylog.txt.2were modified on the 4th May and mylog.txt.3was modified on the 3rd May.
在该目录下有一个mylog.txt.1,mylog.txt.2和mylog.txt.3等当我使用ll命令我可以看到,列出目录在linuxmylog.txt.1和mylog.txt.2进行了修改,在5月4日,并mylog.txt.3于5月3日进行了修改。
When the program checks each of these files however, it is always returning 3rd may. Below is the code that I am using.
然而,当程序检查这些文件中的每一个时,它总是返回 3rd may。下面是我正在使用的代码。
void getFileCreationTime(char *filePath)
{
struct stat attrib;
stat(filePath, &attrib);
char date[10];
strftime(date, 10, "%d-%m-%y", gmtime(&(attrib.st_ctime)));
printf("The file %s was last modified at %s\n", filePath, date);
date[0] = 0;
}
I've tried all the different variations of st_ctime, i.e. st_mtimeand st_atimebut they all return 3rd may.
我已经试过所有不同的变化st_ctime,即st_mtime和st_atime,但他们都返回5月3日。
Thanks for any help you can provide.
感谢您的任何帮助,您可以提供。
采纳答案by Petesh
This is one of those cases where timezones matter. You're getting gmtimeof the st_mtime. You should instead be using localtimeviz.
这是时区很重要的情况之一。你得到gmtime了st_mtime. 您应该改为使用localtimeviz。
strftime(date, 20, "%d-%m-%y", localtime(&(attrib.st_ctime)));
this is because lsuses your timezone information, and when you used gmtimeas part of the display, it deliberately omitted any timezone information.
这是因为ls使用了你的时区信息,当你用作gmtime显示的一部分时,它故意省略了任何时区信息。
回答by unwind
Things to fix:
需要解决的问题:
- Use the proper field, i.e.
st_ctime. - Check that
stat()succeeds before using its result. - Use
strftime(date, sizeof date, ...to remove the risk of using the wrong buffer size.
- 使用适当的字段,即
st_ctime。 stat()在使用其结果之前检查是否成功。- 使用
strftime(date, sizeof date, ...删除使用错误的缓冲区大小的风险。
I first suspected that your filesystem simply didn't support tracking the last-modified time, but since you say that other tools manage to show it, I suspect the code is breaking for whatever reason.
我首先怀疑您的文件系统根本不支持跟踪上次修改时间,但既然您说其他工具设法显示它,我怀疑代码因任何原因而中断。
Could it be that the filenames are not full path names, i.e. they don't include the proper directory prefix?
可能是文件名不是完整路径名,即它们不包含正确的目录前缀吗?
回答by qwertz
This worked fine for me:
这对我来说很好:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/stat.h>
#include <sys/types.h>
void getFileCreationTime(char *path) {
struct stat attr;
stat(path, &attr);
printf("Last modified time: %s", ctime(&attr.st_mtime));
}

