Increase serialization failed to recover saved objects

This is what my class looks like:

#include <iostream>
#include <boost/dynamic_bitset/dynamic_bitset.hpp>
#include <vector>

#include <fstream>

// include headers that implement a archive in simple text format
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/vector.hpp>

using namespace std;
using namespace boost;

class outer{
    friend class boost::serialization::access;
    public:
        int a;
        class inner{
            friend class boost::serialization::access;
            public:
                int a;
                inner(int a){
                    this->a = a;
                }
                template<class Archive>
                void serialize(Archive & ar,const unsigned int version){
                    ar & a;
                }
                inner(){
                }
        };
        vector<inner> inners;
        outer(int a){
            this->a = a;    
            inners.push_back(inner(this->a+1));     
        }
        outer(){
        }

        template<class Archive>
        void serialize(Archive & ar,const unsigned int version){
            ar & a;
            ar & inners;
        }
};

vector<outer> outers;
vector<outer> _outers;

BOOST_CLASS_EXPORT(outer);

int main(int, char*[]) {
    int i;
    std::ofstream ofs("filename.dat");
    for(i=0;i<5;i++)
        outers.push_back(outer(i)); 

    boost::archive::text_oarchive oa(ofs);
    oa << outers;
    std::ifstream ifs("filename.dat");
        boost::archive::text_iarchive ia(ifs);
        // read class state from archive
        ia >> _outers;
  return EXIT_SUCCESS;
}

      

I create a vector of class instances outer

and write it to a text stream (which seems to work fine). But when I read it I get the error:
terminate call after calling the instance "boost :: archive :: archive_exception" what (): input stream error

The above complete code compiled with

g++ -I /path/to/boost test.cpp -lboost_serialization

      

How can I fix this, any ideas?

+3


source to share


1 answer


Is absent:

#include <boost/serialization/export.hpp>

      

Before reading the file, you need to close the output archive / stream:

int main() {
    {
        std::ofstream ofs("filename.dat");

        std::vector<outer> outers(5);
        std::iota(outers.begin(), outers.end(), 0u);

        {
            boost::archive::text_oarchive oa(ofs);
            oa << outers;
        }
    }

    {
        // read class state from archive
        std::vector<outer> _outers;
        std::ifstream ifs("filename.dat");
        boost::archive::text_iarchive ia(ifs);
        ia >> _outers;

        for(auto& outer : _outers) {
            std::cout << "outer " << outer.a << "\n";

            for (auto& inner: outer.inners)
                std::cout << "   inner " << inner.a << "\n";
        }
    }
}

      

Full demo



Live On Coliru

#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/export.hpp>
#include <boost/serialization/vector.hpp>
#include <iostream>
#include <fstream>

class outer {
    friend class boost::serialization::access;

  public:
    int a;
    class inner {
        friend class boost::serialization::access;

      public:
        int a;
        inner(int a) { this->a = a; }
        template <class Archive> void serialize(Archive &ar, unsigned) { ar &a; }
        inner() {}
    };
    std::vector<inner> inners;
    outer(int a = 0) : a(a), inners {a+1} { }

    template <class Archive> void serialize(Archive &ar, unsigned) {
        ar &a;
        ar &inners;
    }
};


BOOST_CLASS_EXPORT(outer)

int main(int, char *[]) {
    {
        std::ofstream ofs("filename.dat");

        std::vector<outer> outers(5);
        std::iota(outers.begin(), outers.end(), 0u);

        {
            boost::archive::text_oarchive oa(ofs);
            oa << outers;
        }
    }

    {
        // read class state from archive
        std::vector<outer> _outers;
        std::ifstream ifs("filename.dat");
        boost::archive::text_iarchive ia(ifs);
        ia >> _outers;

        for(auto& outer : _outers) {
            std::cout << "outer " << outer.a << "\n";

            for (auto& inner: outer.inners)
                std::cout << "   inner " << inner.a << "\n";
        }
    }
}

      

Printing

outer 0
   inner 1
outer 1
   inner 2
outer 2
   inner 3
outer 3
   inner 4
outer 4
   inner 5

      

+2


source







All Articles