开发者

boost c++ library object serialization problem

The error was:

开发者_如何学编程
terminate called after throwing an instance of 'boost::archive::archive_exception
what(): input stream error
Aborted

I have these code in my main.c

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

"FILEX" may or may not exist before, is this the cause of the error? or it is because I implemented the serialize method of Object class in a wrong way?


My favorite reference page when using ifstream: http://www.cplusplus.com/reference/iostream/ifstream/

You're trying to open "FILEX" twice -- I assume that's not what you want to do. I'm not familiar with boost::archive, but you can at least check if ifs is usable:

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);
    }
}


file should exist, only in this case you can deserialize something from it. as @Tim suggested, just check if file was opened successfully.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜