用c语言编写一个程序用标准IO提供的函数接口实现文件的拷贝。
下面是一个使用C语言编写的文件拷贝程序:
#include <stdio.h>
#include <stdlib.h>
#define BUFFER_SIZE 4096
int main() {
FILE *source_file, *dest_file;
char buffer[BUFFER_SIZE];
size_t num_read;
// 打开源文件
source_file = fopen("source.txt", "rb");
if (source_file == NULL) {
printf("无法打开源文件。\n");
return 1;
}
// 创建并打开目标文件
dest_file = fopen("destination.txt", "wb");
if (dest_file == NULL) {
printf("无法创建目标文件。\n");
return 1;
}
// 拷贝文件内容
while ((num_read = fread(buffer, 1, BUFFER_SIZE, source_file)) > 0) {
fwrite(buffer, 1, num_read, dest_file);
}
// 关闭文件
fclose(source_file);
fclose(dest_file);
printf("文件已成功拷贝。\n");
return 0;
}
请将源文件命名为source.txt,程序将把源文件拷贝到名为destination.txt的目标文件中
原文地址: https://www.cveoy.top/t/topic/iqfY 著作权归作者所有。请勿转载和采集!