在 C 中复制后文件检查和不同

  • 本文关键字:检查和 文件 复制 c
  • 更新时间 :
  • 英文 :


我编写一个程序将一个文件复制到另一个文件,但在复制后它给了我不同的校验和而不是实际的校验和。

如果文件包含EOF or null字符,我想将一个文件复制到另一个文件,那么我仍然需要将整个文件从一个文件复制到另一个文件(例如:zip文件,tar文件)

#include<stdio.h>
    int main()
    {
        FILE *p, *q;
        char file1[20], file2[20];
        const int BUF_SIZE = 1024;
        unsigned char buf[BUF_SIZE];
        printf("nEnter the source file name to be copied:");
        gets(file1);
        p = fopen(file1, "r");
        if (p == NULL )
        {
            printf("cannot open %s", file1);
            exit(0);
        }
        printf("nEnter the destination file name:");
        gets(file2);
        q = fopen(file2, "w");
        if (q == NULL )
        {
            printf("cannot open %s", file2);
            exit(0);
        }
        fseek(p, 0, SEEK_END);
        unsigned int left_to_copy = ftell(p);
        while (left_to_copy > BUF_SIZE)
        {
            fread(buf, BUF_SIZE, 1, p);
            fwrite(buf, BUF_SIZE, 1, q);
            left_to_copy -= BUF_SIZE;
        }
        fread(buf, left_to_copy, 1, p);
        fwrite(buf, left_to_copy, 1, q);
        printf("nCOMPLETED");
        fflush(p);
        fflush(q);
        fclose(p);
        fclose(q);
        return 0;
    }

使用了上面的代码,但目标文件给了我不同的校验和意味着文件没有像源一样复制。

谢谢

你应该使用二进制模式:使用 "wb""rb" 作为第二个参数fopen()

最新更新