用c语言编写程序实现文件的复制功能
时间: 2024-03-21 20:38:18 浏览: 14
好的,下面是一个简单的使用 C 语言实现文件复制的程序:
```c
#include <stdio.h>
int main() {
FILE *src_file, *dst_file;
char src_path[100], dst_path[100];
char ch;
printf("请输入源文件路径:");
scanf("%s", src_path);
printf("请输入目标文件路径:");
scanf("%s", dst_path);
src_file = fopen(src_path, "rb");
if (src_file == NULL) {
printf("源文件打开失败!\n");
return 0;
}
dst_file = fopen(dst_path, "wb");
if (dst_file == NULL) {
printf("目标文件创建失败!\n");
return 0;
}
while ((ch = fgetc(src_file)) != EOF) {
fputc(ch, dst_file);
}
fclose(src_file);
fclose(dst_file);
printf("文件复制成功!\n");
return 0;
}
```
该程序实现的功能是从用户输入的源文件路径和目标文件路径中打开源文件和创建目标文件,然后逐个字节地从源文件中读取文件内容,再逐个字节地将文件内容写入到目标文件中,最后关闭源文件和目标文件。