C++复制文件的最可靠方法
C++ most robust way to copy a file
好的,所以我知道磁盘写入错误非常罕见,所以请回顾一下,因为我正在使用的数据非常重要(如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函数中做复杂的事情会大大增加错误和/或漏报的可能性。
相关文章:
- 有可能在Armadillo中复制MATLAB circshift方法吗
- 复制几乎为空的数组的最快方法
- 是否可以将不可复制的成员用作使对象不可复制的替代方法?
- 使用 memcpy 将矢量数据复制到 wstring 的正确方法
- C++将所有元素从矢量复制到地图/unordered_map的最佳方法
- 通过基类接受方法转发派生 UniquePtr 的右值会移动引用而不是复制
- 将 std:set<int32_t> 复制到 std::set <uint32_t>的好方法
- 使变量不可复制的紧凑方法
- 删除复制构造函数的 Intel 13.1.2 中不良C++行为的解决方法
- 将一种数据类型的向量复制到同一数据类型的结构向量中的有效方法是什么
- 在c++中的复制构造函数/一个声明语句中的初始化的延续中使用chain方法
- 有没有一种方法可以在不复制数据的情况下从string_view创建字符串流
- 运算符重载C++、(+、-、*、/ 等)有没有比复制、替换和粘贴更聪明的方法?
- 是否有任何常规方法可以通知 STL 移动和复制构造函数?
- 尝试使用 Qt 库中的 QPixmap 将图像拆分为多个块。关于他的复制方法的工作方式,我有什么不明白的吗?
- 我可以制作一个对象方法,如果单独调用,它将自行修改,但如果在复制初始化期间调用,则会返回一个新对象?
- 我正在考虑为"A"类创建复制构造函数,但我不确定采用哪种方法
- 复制构造函数并从方法中获取数据
- 是否有其他方法将.dll文件从一个项目复制到我的启动项目中的可执行文件旁边
- 如何通过派生类的方法复制来设置基类字段