C++复制文件的最可靠方法

C++ most robust way to copy a file

本文关键字:方法 复制 文件 C++      更新时间:2023-10-16

好的,所以我知道磁盘写入错误非常罕见,所以请回顾一下,因为我正在使用的数据非常重要(如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;
}


那么,如果我的代码以最好的方式走在正确的轨道上,那么我的代码可以做些什么来改进它呢?但是,如果我的代码完全不符合最佳解决方案,那么最佳解决方案是什么?请注意,这个问题本质上是关于检测罕见的磁盘写入错误,用于复制非常非常非常(等)重要数据的应用。

我只会复制文件而不进行任何特殊检查,最后我会读取文件并将其哈希值与预期的哈希值进行比较。对于哈希函数,我会使用 MD5 或 SHA-1。

#include <boost/filesystem.hpp>
#include <iostream>
int main()
{
    try
    {
        boost::filesystem::copy_file( "C:\Users\Admin\Desktop\example file.txt",
                                      "C:\Users\Admin\Desktop\example copy.txt" );
    }
    catch ( boost::filesystem::filesystem_error const & ex )
    {
        std::cerr << "Copy failed: " << ex.what();
    }
}

这将调用可以说是最健壮的可用实现 - 操作系统提供的实现 - 并报告任何故障。


我的观点是:

首先,您保存的数据最终损坏的可能性非常小。

任何可能真正成为问题的应用程序都应该在冗余存储上运行,即 RAID 阵列、执行校验和的文件系统(如 Btrfs、ZFS)等,再次显着降低故障的可能性。

另一方面,在本土I/O函数中做复杂的事情会大大增加错误和/或漏报的可能性。

相关文章: