0

あるファイルを別のファイルにコピーするプログラムを作成しましたが、コピー後に実際のチェックサムとは異なるチェックサムが表示されます。

ファイルに文字が含まれている場合、あるファイルを別のファイルにコピーしたいのですが、ファイル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;
    }

上記のコードを使用しましたが、宛先ファイルが異なるチェックサムを提供します。これは、ファイルがソースのようにコピーされていないことを意味します。

ありがとう

4

1 に答える 1

2

バイナリ モードを使用する必要があります。2 番目のパラメータとして"wb"andを使用します。"rb"fopen()

于 2013-11-14T14:12:34.037 に答える