C语言代码,检测指定路径下是否存在文件A,如果存在则将文件A重命名为B
时间: 2024-05-06 07:21:04 浏览: 4
以下是C语言代码,可以检测指定路径下是否存在文件A,如果存在则将文件A重命名为B:
```
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
bool file_exists(char *filename) {
FILE *file;
if (file = fopen(filename, "r")) {
fclose(file);
return true;
} else {
return false;
}
}
int main() {
char *old_filename = "path/to/fileA";
char *new_filename = "path/to/fileB";
if (file_exists(old_filename)) {
if (rename(old_filename, new_filename) != 0) {
printf("Failed to rename file.\n");
} else {
printf("File renamed successfully.\n");
}
} else {
printf("File does not exist.\n");
}
return 0;
}
```
希望这个代码可以帮到你!请问还有什么我可以为你做的吗?