我的程序需要用户输入并检查是否在一分钟内修改了文件,而不是用户输入。我试图使用stat()函数,如下所示获得文件的最后修改时间:正确使用stat()函数访问m_time的方法
注:op_mmin是以分钟为单位的用户输入
struct stat buf;
stat(sub_directory, &buf);
if((time(NULL) - buf.st_mtime) < atoi(op_mmin) * 60) // if the file was modified in less time ago than specified
{
printf("%d\n", buf.st_mtime); // this print statement simply is used to check what the st_mtime is.
printf("%s\n", full_path); // print the full path of the file
}
我的代码目前打印像看似随机负数-1036294304
和-367577248
。我刚刚创建了它正在搜索的文件,因此time(NULL) - buf.st_mtime
应该比较小。
'buf.st_mtime'是'time_t',但是您将它打印为'int'(使用'%d'格式)。以防万一,使用'%lld'并明确地将其转换为'long long'。 – keltar