内容:编写一个C程序time_travelling_files.c,它接受一个文件列表作为命令行参数。
您的程序应该对每个文件检查文件的访问时间或文件的修改时间是否在未来。
如果文件的访问时间或文件的修改时间是在未来,您的程序应该打印一个消息的格式如下。
图片如下:
(绿色hint部分提供了能给出当前时间的代码)
还请帮忙看看这个,这个文件时间不知道怎么获得…谢谢!

C语言寻找时间旅行文件
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
2条回答 默认 最新
- 技术专家团-小桥流水 2022-04-22 14:52关注
获取文件的最后修改时间跟当前系统时间进行比较就是了,代码如下:
#include <stdio.h> #include <sys/stat.h> #include <time.h> int main(int argc,void* argv[]) { int i=0; struct stat statbuf; //get current time struct timespec tn; clock_gettime(CLOCK_REALTIME,&tn); if(argc < 2) { printf("less param\n"); return 0; } for(i=1;i<argc;i++) { if(stat(argv[i],&statbuf) != -1) { if(tn.tv_sec < statbuf.st_mtime || tn.tv_sec < statbuf.st_atime ) { printf("%s has a timestamp that is in the future\n",argv[i]); } } } return 0; }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报 编辑记录