我一直在尝试使用fread
和write
将2个二进制文件相互复制。我已经阅读了几篇文章,解释了它们的工作原理,但是我不明白我的错误是什么。
我尝试过将字符fread
切换到int
上,以使-1
不会干扰该过程,但是似乎没有用。
我查找了一些链接以寻找答案:
Why is “while (!feof(file))” always wrong?
https://www.tutorialspoint.com/c_standard_library/c_function_fwrite.htm
https://www.tutorialspoint.com/c_standard_library/c_function_fread.htm
Copying Binary Files
Copying data from one text file to another in C
while (tempNum != EOF) {
fread(tempNum, 1, 1, fptr);
fwrite(tempNum, 1, 1, fp);
}
供您测试的示例:
#include <string.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/stat.h>
FILE *fptr;
FILE *fp;
int main(int argc, char** argv)
{
int flag = 0;
int exist = 0;
char currentChar = 0;
int tempNum = 0;
fptr = 0;
fp = fopen(*(argv + 3), "r");
fptr = fopen(*(argv + 2), "r");
char temp_array[10000] = { 0 };
if (fptr == NULL)
{
printf("we cannot extract from nothing.");
flag++;
}
else if (fp != NULL)
{
printf("This file already exists. If you would like to overwrite it enter 0. %s");
scanf("%d", &flag);
}
if (!strcmp(*(argv + 1), "textCopy") && flag == 0)
{
fclose(fp);
fclose(fptr);
printf("A");
fp = fopen(*(argv + 3), "w");
fptr = fopen(*(argv + 2), "r");
printf("%s , %s", *(argv + 2), *(argv + 3));
while (currentChar != EOF)
{
printf("a");
currentChar = fgetc(fptr);
fputc(currentChar, fp);
}
fclose(fp);
fclose(fptr);
}
else if (!strcmp(*(argv + 1), "binaryCopy") && flag == 0)
{
printf("A");
fptr = fopen(*(argv + 2), "r");
fp = fopen(*(argv + 3), "w");
while (tempNum != EOF)
{
fread(tempNum, 1, 1, fptr);
fwrite(tempNum, 1, 1, fp);
}
}
getchar();
getchar();
return 0;
}
预期:获得2个相同的文件。
实际:我成功复制了文件的前6个字节(我使用了十六进制车间),但是之后Visual Studio崩溃了,并说传递给函数fread
的参数认为输入是致命的。
答案 0 :(得分:1)
首先,我尝试最小化您的程序。我假设您要执行复制二进制数据的路径。为了简单起见,我删除了无关的内容,并使用了硬编码的输入和输出文件。
(原始代码中有一些不相关的错误,此处未提及。)
#include <string.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/stat.h>
FILE *fptr;
FILE *fp;
int main(int argc, char** argv)
{
int tempNum = 0;
char temp_array[10000] = { 0 };
fptr = fopen("input.txt", "r");
/* TODO: handle possible error */
fp = fopen("output.txt", "w");
/* TODO: handle possible error */
/* FIXME: This is not the correct way to check for EOF. */
while (tempNum != EOF)
{
/* FIXME: The first argument must be a buffer for reading the data. You should check the return code. */
fread(tempNum, 1, 1, fptr);
/* FIXME: The first argument must be a buffer containing the data. You should check the return code. */
fwrite(tempNum, 1, 1, fp);
}
/* TODO: close files */
return 0;
}
现在是可以工作并处理一些错误的修改版本。
#include <string.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/stat.h>
FILE *fptr = NULL;
FILE *fp = NULL;
int main(int argc, char** argv)
{
int tempNum = 0;
char temp_array[10000] = { 0 };
fptr = fopen("input.txt", "r");
if(fptr == NULL) /* handle possible error */
{
perror("fopen(input.txt)");
return 1;
}
fp = fopen("output.txt", "w");
if(fp == NULL) /* handle possible error */
{
perror("fopen(output.txt)");
fclose(fptr);
return 1;
}
do
{
tempNum = fread(temp_array, 1, 1, fptr);
if(tempNum > 0)
{
if(fwrite(temp_array, 1, tempNum, fp) != tempNum)
{
perror("fwrite");
fclose(fptr);
fclose(fp);
return 1;
}
}
}
while(tempNum > 0);
/* after fread() returned less than we expected, check for error */
if(ferror(fptr))
{
perror("fread");
fclose(fptr);
fclose(fp);
return 1;
}
/* When we reach this point we can assume the loop ended on EOF */
fclose(fptr);
fclose(fp);
return 0;
}
答案 1 :(得分:0)
这是我经常遇到的任务,因此我有一个框架代码片段:
typedef unsigned char byte;
byte *buffer = NULL;
size_t len;
fseek(input_file, 0, SEEK_END);
len = ftell(input_file);
rewind(input_file);
buffer = malloc(len + 1);
if(NULL == buffer)
{
// handle malloc failure
}
fread(buffer, 1, len, input_file);
fclose(input_file);
fwrite(buffer, 1, len, output_file);
fclose(output_file);
free(buffer);
buffer = NULL;
请注意,如果您的文件太大,malloc
可能会失败,并且最好的解决方案是在循环中一次缓冲文件的大块。