C 尝试使用fread和fwrite复制文件时出现问题

C 尝试使用fread和fwrite复制文件时出现问题,c,C,我一直在尝试使用fread和write将两个二进制文件一个复制到另一个。我读过几篇文章解释了它们是如何工作的,但我不明白我的错误是什么 我尝试过将char-fread转换为int,这样-1就不会干扰这个过程,但它似乎不起作用 我查找了一些链接以搜索答案: 要测试的示例: #include <string.h> #include <stdio.h> #include <sys/stat.h> #include <sys/types.h> #inc

我一直在尝试使用fread和write将两个二进制文件一个复制到另一个。我读过几篇文章解释了它们是如何工作的,但我不明白我的错误是什么

我尝试过将char-fread转换为int,这样-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 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个相同的文件


<>实际:我成功复制了我使用HEX车间的文件的前6个字节,但是VisualStudio崩溃了,并说传递给函数Frad的参数考虑了输入的致命性。

首先,我试着最小化你的程序。我假设您希望执行复制二进制数据的路径。为了简单起见,我删除了不相关的内容,并使用硬编码的输入和输出文件

在这里没有提到的原始代码中有一些不相关的错误

#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;
}

这是我经常遇到的一项任务,因此我有一个框架代码段:

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可能会失败,最好的解决方案是在循环中一次缓冲文件块。

检查函数的结果。@Lundin它的结果是什么意思?返回值?不确定是否存在真正的副本,但要解决此问题,所有操作都应正常工作:首先检查fread的结果。如果它返回的值不是您案例中预期的元素数1,您可以在之后检查feoffptr或ferrorfptr。这甚至不可能在没有警告的情况下编译。
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;