在抛出'std::runtime_error' what() 实例后调用终止:filebuf 和 ostream 的 I/O 错误

Taihouuuuuuuuuuu

我正在尝试序列化几个文本文件,我有一个这样的功能:

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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

得到错误“抛出'std :: bad_alloc'what():whatstd :: bad_alloc实例后终止调用”

抛出“std::invalid_argument”实例后调用终止 what(): leetcode 问题中的 stoi 错误

在抛出 'std::out_of_range' what() 实例后调用终止:basic_string::at: __n 错误

C ++中的文件IO错误。引发'std :: length_error'what()实例后调用终止终止what():basic_string :: resize

出现错误:抛出'std :: bad :: alloc'what():what std :: bad_alloc实例后终止终止

在抛出“std::regex_error”what() 实例后调用终止:括号未关闭

C++ 中的错误:“在抛出 'std::length_error' what() 实例后调用终止:basic_string::_M_replace_aux”

在带有Rcpp的Ubuntu Xenial上抛出std :: runtime_error时出现段错误

抛出'std :: regex_error'实例后调用终止

在抛出“std::system_error”实例后调用 Tensorflow 终止

C++ 抛出错误“在抛出‘std::bad_alloc’的实例后终止调用”

当我运行我的代码时,我不断收到此错误“在抛出 'std::bad_alloc' what(): std::bad_alloc 实例后调用终止”

std::stoi() 错误——“在抛出 'std::invalid_argument' 实例后终止调用”

在抛出 std::exception 实例后调用终止

C ++错误:抛出'std :: bad_alloc'实例后终止调用

抛出'std :: out_of_range'what():vector :: _ M_range_check实例后终止调用

在抛出'std :: length_error'what()实例之后调用终止终止what():basic_string :: __ S_create

抛出“std::system_error”线程池实例后调用终止

将字母向前移动 3 个字母的程序,错误:在抛出 'std::out_of_range' 实例后调用终止

抛出'std :: out_of_range'实例后调用终止

"if (argc < 2 || argc > 2)" 应该有 2 个参数吗?& 在抛出“std::out_of_range”错误实例后调用终止

为什么“在抛出一个 'std::out_of_range' what(): basic_string::at: __n (which is 0) >= this->size() (which is 0) 实例后终止调用?

是std :: runtime_error的副本构造函数和副本赋值吗?

c++ 在抛出'std::out_of_range' std::vector 的实例后调用终止

Cmake exe 文件只是在我的系统中在 Sunshine 项目中运行 [在抛出 std::filesystem::__cxx11::filesystem_error 实例后终止调用]

在C ++中抛出'std :: out_of_range'实例后调用终止

为什么在抛出'std :: bad_alloc'实例后终止调用?

Tensorflow MNIST:抛出'std :: bad_alloc'实例后调用终止

抛出'std::bad_alloc'的实例后调用C++终止