【发布时间】:2014-06-26 18:37:39
【问题描述】:
如何修改此代码以删除指定目录中至少 7 天前的所有文件。这是我拥有的以下源代码:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <direct.h>
#include <cstring>
int main(int argc, char* argv[1]) {
if (argc < 2) {
std:cerr<<"Usage: " << argv[0] << " <filename>" << std::endl;
return 1;
}
struct stat buffer;
for (int i; i < argc; ++i) {
int result = stat(argc[i], &buffer);
if (result != 0) {
std::cerr << argv[i] << ": "<<stderror(errno) << std::endl;
continue;
}
char datetime[100] = [0];
const struct tm* time = localtime(sbuffer.st_mtime);
result = strftime(datetime, 100, "%c", time);
std::cout << argv[i] << ": " << datetime << std::endl;
}
return 0;
}
如何获取文件的年龄并删除超过 7 天的文件?任何建议将不胜感激!
【问题讨论】: