开发者

boost c++ library object serialization problem

开发者 https://www.devze.com 2023-02-10 14:33 出处:网络
The error was: 开发者_如何学编程terminate called after throwing an instance of \'boost::archive::archive_exception

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

精彩评论

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