c语言文件拷贝
时间: 2023-08-25 15:18:20 浏览: 61
你可以使用C语言中的文件操作函数来进行文件拷贝。下面是一个简单的文件拷贝的示例代码:
```c
#include <stdio.h>
int main() {
FILE *source_file, *target_file;
char source_path[100], target_path[100];
char buffer[1000];
int bytes_read;
// 获取源文件和目标文件路径
printf("请输入源文件路径:");
scanf("%s", source_path);
printf("请输入目标文件路径:");
scanf("%s", target_path);
// 打开源文件和目标文件
source_file = fopen(source_path, "rb");
if (source_file == NULL) {
printf("无法打开源文件!\n");
return 1;
}
target_file = fopen(target_path, "wb");
if (target_file == NULL) {
printf("无法打开目标文件!\n");
fclose(source_file);
return 1;
}
// 拷贝文件内容
while ((bytes_read = fread(buffer, 1, sizeof(buffer), source_file)) > 0) {
fwrite(buffer, 1, bytes_read, target_file);
}
// 关闭文件
fclose(source_file);
fclose(target_file);
printf("文件拷贝完成!\n");
return 0;
}
```
该代码会要求用户输入源文件路径和目标文件路径,然后打开这两个文件进行拷贝。注意要使用二进制模式打开文件,以确保正确拷贝二进制文件。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)