gpt4 book ai didi

C++ 最健壮的文件复制方式

转载 作者:行者123 更新时间:2023-11-28 00:07:19 25 4
gpt4 key购买 nike

好的,所以我知道磁盘写入错误非常罕见,所以请忽略它,因为我正在处理的数据非常重要(比如 SSID 很重要)。因此,我想使用绝对最小的内存量以绝对最可靠的方式复制文件。到目前为止,这是我所得到的。它吸收了很多内存,但我找不到来源。它的工作方式是通过多次重新检查直到得到确认的结果(它可能会大大增加误报错误的数量,但它可能会大大降低实际错误的可能性)。此外,底部的 sleep 让您有时间使用 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。

关于C++ 最健壮的文件复制方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34812143/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com