c++ - boost c++库对象序列化问题

标签 c++

错误是:

terminate called after throwing an instance of 'boost::archive::archive_exception
what(): input stream error
Aborted

我的 main.c 中有这些代码

Object *obj = new Object();
{
    std::ifstream ifs("FILEX");
    boost::archive::text_iarchive ia(ifs);
    ia >> *obj;
}

“FILEX”以前可能存在也可能不存在,这是错误的原因吗?或者是 因为我用错误的方式实现了Object类的serialize方法?

最佳答案

使用 ifstream 时我最喜欢的引用页面: http://www.cplusplus.com/reference/iostream/ifstream/

您正在尝试打开“FILEX”两次——我认为这不是您想要做的。我不熟悉 boost::archive,但你至少可以检查 ifs 是否可用:

Object *obj = new Object();
{
    std::ifstream ifs("FILEX");
    if (ifs.good()) {
        boost::archive::text_iarchive ia(ifs);
        ia >> *obj;
    } else {
        // throw an error or something
        assert(false);
    }
}

关于c++ - boost c++库对象序列化问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4975146/

相关文章:

c++ - C++ 中 vector 的动态内存分配

c++ - OpenGL3 : Clean-up after failed shader compilation

c++ - 如何让 Qt Creator 添加 CEF 框架到 Mac OS X 上的 DEST_DIR?

c++ - 使 Qt 应用程序在最后一个窗口关闭时不退出

c++ - libstdc++ 在 ubuntu 64 位中崩溃

c++ - Mac OS X Mavericks 上的 libc++ header 在哪里?

C++ vector 下标超出范围错误 1221

c++ - 奇怪的 ifstream 行为

C++ 错误 C2040 : 'ip' : 'const char *' differs in levels of indirection from 'std::string' ?

c++ - 大规模递归函数中的段错误