在抛出 'std::runtime_error' 的实例后终止调用 what(): Filebuf 和 ostream 的 I/O 错误
terminate called after throwing an instance of 'std::runtime_error' what(): I/O error with filebuf & ostream
我正在尝试序列化几个文本文件,我有一个这样的函数:
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
好吧,我犯了一个错误。我没有初始化filebuf*
.
所以我更改了filebuf* fbCipherR = new filebuf();
,并收到一条新的错误消息:
terminate called after throwing an instance of 'std::runtime_error'
what(): I/O error
Aborted (core dumped)
您应该在堆栈而不是堆中分配变量,因为这样它们就是默认构造的并自动销毁。
filebuf fbCipherR;
string filenameR = "../serialization/pixels/px" + to_string(index) + "R.txt";
fbCipherR.open(filenameR.c_str(), std::ios::out|std::ios::binary);
ostream osCipherR(fbCipherR);
encryptedPx[0].Ciphertext::save(osCipherR);
fbCipherR.close();
程序已终止,因为存在未捕获的异常。您应该在每个线程中处理异常。如何捕获异常。
您的异常可能是由于打开文件失败,您应该在使用之前检查缓冲区/流的状态。
您可以通过使用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 filen";
}
else
{
encryptedPx[0].Ciphertext::save(osCipherR);
}
//green
//blue
delete[] encryptedPx;
}
尝试使用此方法初始化 filebuf。
std::ifstream is;
std::filebuf * fb = is.rdbuf();
fb->open ("test.txt",std::ios::out|std::ios::app);
// >> appending operations here <<
fb->close();
对于字符串,您不需要创建指向字符串的指针,因为您只是在此函数中使用字符串,只需像这样编写即可
//string* filenameR = new string("../serialization/pixels/px" + to_string(index) + "R.txt");
//use a local variable string instead of a "new" string since you are not using this string anymore after this function
string filenameR = "../serialization/pixels/px" + to_string(index) + "R.txt";
相关文章:
- 多个文件的内存分配错误"在抛出 'std :: bad_alloc' what (): std :: bad_alloc 的实例后终止调用" [C++]
- 什么是 std::exception::what() 以及为什么要使用它?
- what(): basic_string::_M_construct null not valid
- what(): basic_string::_M_construct 空无效错误
- What is unordered_set in C++
- 自定义 std::fstream,std::filebuf 的溢出和下溢函数未为每个字符调用
- 在抛出 'std::runtime_error' 的实例后终止调用 what(): Filebuf 和 ostream 的 I/O 错误
- 自定义runtime_error,如果我在 #what 中使用#c_str(),则错误输出始终为空
- 在抛出 what() 的实例后调用'std::logic_error'终止:basic_string::_M_construct 空无效
- "what does ":*** [可执行文件] 错误 1 " mean ?"
- What is the std::chrono::time_point equivalent of std::numer
- WIC 工厂将始终在 Windows7 上为 nullptr("What's a Creel?"教程中使用)
- 自定义派生的 std::exception 类的 'what' 函数返回神秘的废话
- What is std::vector::_emplace_back_slow_path / std::vector::
- 在抛出 'std::invalid_argument' 的实例后终止调用 what(): stoi (找不到问题所在)
- "terminate called after throwing an instance of std::invalid_argument' what(): stoi ?"
- "Include What You Use"
- 获取错误:在抛出"std::bad::alloc"的实例后终止调用 what(): std::bad_alloc
- 在抛出 'std::bad_weak_ptr' 的实例后终止调用 what(): bad_weak_ptr?
- X3, what is attr_gen?