我已经尝试过这个:
if (argc > 2) {
int i;
for(i = 0; i < argc; i++) {
if(i != 0) {
rename_file(argv[i]);
}
}
}
如果我传入两个文件名,仅重命名第一个,这个循环有什么问题?
编辑:这是 rename_file 函数(我认为它应该有效)
void rename_file(char const *filename) {
struct tm *clock;
struct stat attrib;
const char *dot = strrchr(filename, '.');
const char *ext = NULL;
if (dot && *dot) {
ext = dot + 1;
}
stat(filename, &attrib);
clock = gmtime(&(attrib.st_mtime));
char newname[250];
if (!ext) {
printf("ERROR: No File Extenstion");
} else {
sprintf(newname, "%02d_%02d_%02d.%s", clock->tm_mday, clock->tm_mon, clock->tm_year + 1900, ext);
}
rename(filename, newname);
}
最佳答案
试试这个:
if (argc > 2) {
for(int i = 1; i < argc; i++) {
rename_file(argv[i]);
}
}
关于c - 数组循环不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5371510/