我正在尝试编写一个覆盖二进制文件的函数。现在我编写了这个覆盖文本文件的函数,它完全正常工作:
void myCopy(char source_file[50], char dest_file[50])
{
FILE *fp1, *fp2;
char a;
fp1 = fopen(source_file, "rt");
if (fp1 == NULL)
{
printf("Can't open this file, either it doesn't exist or doesn't contain anything.\n");
}
fp2 = fopen(dest_file, "wt");
if (fp2 == NULL)
{
printf("Can't open this file, either it doesn't exist or doesn't contain anything.\n");
}
while ((a = fgetc(fp1)) != EOF)//While a failure did not accure.
{
fputc(a, fp2);
}
fclose(fp1);
fclose(fp2);
}
我的问题是,有没有办法在不使用fputc,fgetc,fscanf和fprintf的情况下对所有二进制文件执行相同的操作?
答案 0 :(得分:0)
如果你删除了“二进制文件”,你的代码将是正确的。来自fopen
选项字符串的字符。但是您可以使用fread
/ fwrite
函数和缓冲区来提高复制速度。例如:
const size_t buffer_size = 8192;
char buffer[buffer_size];
while (true) {
size_t read = fread(buffer, sizeof(char), buffer_size, fp1);
if (read > 0)
fwrite(buffer, sizeof(char), buffer_size, fp2);
else
break;
}