我正在尝试序列化几个文本文件,我有一个这样的功能:
void to_file_px(Ciphertext* encryptedPx, int index) {
// Serialize Pixel i
//red
filebuf* fbCipherR; // EDIT: THIS LINE IS PROBLEMATIC
string* filenameR = new string("../serialization/pixels/px" + to_string(index) + "R.txt");
fbCipherR -> open((*filenameR).c_str(), std::ios::out|std::ios::binary);
ostream* osCipherR = new ostream(fbCipherR);
encryptedPx[0].Ciphertext::save((*osCipherR));
fbCipherR -> close();
delete filenameR;
delete fbCipherR;
delete osCipherR;
//green
//blue
delete[] encryptedPx;
}
但是,此函数会导致错误为Segmentation fault (core dumped)
。
我可以知道究竟是什么导致了错误?
注意:Ciphertext::save
来自 Microsoft SEAL
好吧,我犯了一个错误。我没有初始化filebuf*
。
所以我改变了filebuf* fbCipherR = new filebuf();
,我收到了一条新的错误信息:
terminate called after throwing an instance of 'std::runtime_error'
what(): I/O error
Aborted (core dumped)
您的异常可能是由于打开文件失败,您应该在使用之前检查缓冲区/流的状态。
您可以通过使用ofstream
而不是filebuf
将所有内容放在堆栈上而不是堆分配来简化代码:
void to_file_px(Ciphertext* encryptedPx, int index) {
// Serialize Pixel i
//red
string filenameR = "../serialization/pixels/px" + to_string(index) + "R.txt";
ofstream osCipherR(filenameR.c_str(), std::ios::out|std::ios::binary);
if (!osCipherR)
{
std::cout << "error opening output file\n";
}
else
{
encryptedPx[0].Ciphertext::save(osCipherR);
}
//green
//blue
delete[] encryptedPx;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句