fread/fwrite文件拷贝

// 文件拷贝

#include <stdio.h>

int main(int argc, char *argv[])
{
    FILE *file_A = fopen("A.txt", "r");
    if (NULL == file_A)
    {
        printf("fopen file_A error\n");
        return 0;
    }

    FILE *file_B = fopen("test.txt", "w+");
    if (NULL == file_B)
    {
        printf("fopen file error\n");
        return 0;
    }

    while (1)
    {
        char str[8] = {0};

        fread(str, sizeof(str), 1, file_A);
        if (ferror(file_A))
        {
            printf("文件读取错误\n");
            return 0;
        }
        else if (feof(file_A))
        {
            printf("文件读取完毕\n");
            break;
        }

        fwrite(str, sizeof(str), 1, file_B);
        if (ferror(file_B))
        {
            printf("文件写入出错\n");
            return 0;
        }
    }

    fclose(file_A);
    fclose(file_B);

    return 1;
}

 

posted @ 2023-05-09 00:57  jason8826  阅读(36)  评论(0)    收藏  举报