0

さて、ディスク書き込みエラーが非常にまれであることはわかっています。私が扱っているデータは非常に重要です (SSID のような重要なものです)。したがって、絶対に最小限のメモリ量を使用して、最も堅牢な方法でファイルをコピーしたいと考えています。これまでのところ、これは私が得た限りです。大量のメモリを消費しますが、ソースが見つかりません。その仕組みは、確認結果が得られるまで何回も再チェックすることです (エラーの誤検知の数が大幅に増える可能性がありますが、実際のエラーの可能性は大幅に減少する可能性があります)。また、下部のスリープは、Windows タスク マネージャーを使用してプログラムの全体的なパフォーマンスを分析する時間を確保するためのものです。


#include <cstdio>   // fopen, fclose, fread, fwrite, BUFSIZ
#include <cstdlib>
#include <unistd.h>
#include <iostream>

using namespace std;

__inline__ bool copy_file(const char* From, const char* To)
{
    FILE infile = (*fopen(From, "rb"));
    FILE outfile = (*fopen(To, "rwb+"));
    setvbuf( &infile, nullptr, _IONBF, 0);
    setvbuf( &outfile, nullptr, _IONBF, 0);

    fseek(&infile,0,SEEK_END);
    long int size = ftell(&infile);
    fseek(&infile,0,SEEK_SET);
    unsigned short error_amount;
    bool success;
    char c;
    char w;
    char l;

    for ( fpos_t i=0; (i != size); ++i ) {
        error_amount=0;
        fsetpos( &infile, &i );
        c = fgetc(&infile);
        fsetpos( &infile, &i );
        success=true;
        for ( l=0; (l != 126); ++l ) {
            fsetpos( &infile, &i );
            success = ( success == ( fgetc(&infile)==c ) );
        }
        while (success==false) {
            fsetpos( &infile, &i );
            if (error_amount==32767) {
                cerr << "There were 32768 failed attemps at accessing a part of the file! exiting the program...";
                return false;
            }
            ++error_amount;
            //cout << "an error has occured at position ";
            //printf("%d in the file.\n", (int)i);
            c = fgetc(&infile);
            fsetpos( &infile, &i );
            success=true;
            for ( l=0; (l != 126); ++l ) {
                fsetpos( &infile, &i );
                success = ( success == ( fgetc(&infile)==c ) );
            }
        }



        fsetpos( &infile, &i );
        fputc( c, &outfile);
        fsetpos( &outfile, &i );


        error_amount=0;
        w = fgetc(&infile);
        fsetpos( &outfile, &i );
        success=true;
        for ( l=0; (l != 126); ++l ) {
            fsetpos( &outfile, &i );
            success = ( success == ( fgetc(&outfile)==w ) );
        }
        while (success==false) {
            fsetpos( &outfile, &i );
            fputc( c, &outfile);
            if (error_amount==32767) {
                cerr << "There were 32768 failed attemps at writing to a part of the file! exiting the program...";
                return false;
            }
            ++error_amount;
            w = fgetc(&infile);
            fsetpos( &infile, &i );
            success=true;
            for ( l=0; (l != 126); ++l ) {
                fsetpos( &outfile, &i );
                success = ( success == ( fgetc(&outfile)==w ) );
            }
        }
        fsetpos( &infile, &i );
    }

    fclose(&infile);
    fclose(&outfile);

    return true;
}

int main( void )
{
    int CopyResult = copy_file("C:\\Users\\Admin\\Desktop\\example file.txt","C:\\Users\\Admin\\Desktop\\example copy.txt");

    std::cout << "Could it copy the file? " << CopyResult << '\n';

    sleep(65535);
    return 1;
}


では、私のコードが最善の方法で正しい軌道に乗っている場合、それを改善するために私のコードで何ができるのでしょうか? しかし、私のコードが最適な解決策と完全に一致していない場合、最適な解決策は何ですか? この質問は基本的に、非常に非常に重要なデータをコピーするアプリケーションのまれなディスク書き込みエラーの検出に関するものであることに注意してください。

4

2 に答える 2

2

特別なチェックを行わずにファイルをコピーするだけで、最後にファイルを読み取り、そのハッシュ値を期待値と比較します。ハッシュ関数には、MD5 または SHA-1 を使用します。

于 2016-01-15T13:37:54.430 に答える