c++ 文件的删除、复制与重命名操作 实例
时间: 2024-05-05 17:16:05 浏览: 54
删除文件:
```c
#include <stdio.h>
#include <stdlib.h>
int main() {
char filename[50];
printf("请输入要删除的文件名:");
scanf("%s", filename);
if (remove(filename) == 0) {
printf("文件删除成功!\n");
} else {
printf("文件删除失败!\n");
}
return 0;
}
```
复制文件:
```c
#include <stdio.h>
#include <stdlib.h>
int main() {
char source[50], target[50];
FILE *source_file, *target_file;
printf("请输入源文件名:");
scanf("%s", source);
printf("请输入目标文件名:");
scanf("%s", target);
source_file = fopen(source, "rb");
if (source_file == NULL) {
printf("源文件打开失败!\n");
exit(1);
}
target_file = fopen(target, "wb");
if (target_file == NULL) {
printf("目标文件打开失败!\n");
exit(1);
}
char ch;
while ((ch = fgetc(source_file)) != EOF) {
fputc(ch, target_file);
}
printf("文件复制成功!\n");
fclose(source_file);
fclose(target_file);
return 0;
}
```
重命名文件:
```c
#include <stdio.h>
#include <stdlib.h>
int main() {
char oldname[50], newname[50];
printf("请输入要重命名的文件名:");
scanf("%s", oldname);
printf("请输入新的文件名:");
scanf("%s", newname);
if (rename(oldname, newname) == 0) {
printf("文件重命名成功!\n");
} else {
printf("文件重命名失败!\n");
}
return 0;
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![xlsx](https://img-home.csdnimg.cn/images/20210720083732.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)