我有一个.crl文件,我想复制到另一个位置。从我看到的所有帖子到现在,如果不复制内容就无法完成。有没有什么方法可以将文件传输到cpp中的另一个位置而不复制内容?
我尝试使用通常的fopen方法复制内容。但是数据没有被写入缓冲区。如果没有直接的方法,有人可以告诉我如何读取证书文件并将内容写入另一个位置的另一个文件?
我也试过了fstream方法
std::ofstream dest("destination.crl", std::ios::trunc|std::ios::binary);
if(!dest.good())
{ std::cerr << "error opening output file\n";
//std::exit(2);
}
std::fstream src("sourcec.crl", std::ios::binary);
if(!src.good())
{ std::cerr << "error opening input file\n";
//std::exit(1);
}
dest << src.rdbuf();
if(!src.eof()) std::cerr << "reading from file failed\n";
if(!dest.good()) std::cerr << "writing to file failed\n";
但是它显示了错误: 打开输入文件时出错 从文件中读取失败 写入文件失败
提前谢谢
答案 0 :(得分:0)
我发现了here。
看,这可能会对你有所帮助。由于您不想阅读,我认为您不想自己执行读写操作。
#include <istream>
#include <iostream>
#include <fstream>
#include <iterator>
using namespace std;
int main()
{
fstream f("source.crl", fstream::in|fstream::binary);
f << noskipws;
istream_iterator<unsigned char> begin(f);
istream_iterator<unsigned char> end;
fstream f2("destination.crl",
fstream::out|fstream::trunc|fstream::binary);
ostream_iterator<char> begin2(f2);
copy(begin, end, begin2);
}
答案 1 :(得分:0)
根据你的回答,我写了另一个答案。
对于Window,有函数[CopyFile][1]
。您可以使用此功能复制文件,而无需自己阅读内容。
对于基于linux / unix的,我无法找到直接等效的CopyFile
。
我相信这question可能对您有帮助。