Problem trying to load XML archive
Hello, I am trying to save and load a vector<map<string,map<string,int> > > to an axml archive. I am able to save the xml archive properly, but I cannot read the saved archive. I have examined the XML file that is generated and it does not seem to have any errors. I get the following assertion error when I try to load the file. Any help would be appreciated. Thank you, Thomas /include/boost/serialization/collections_load_imp.hpp:69: void boost::serialization::stl::archive_input_map<Archive, Container>::operator()(Archive&, Container&) [with Archive = boost::archive::xml_iarchive, Container = std::map<std::string, int, std::less<std::string>, std::allocator<std::pair<const std::string, int> >
]: Assertion `result.second' failed.
#include <boost/archive/xml_oarchive.hpp> #include <boost/archive/xml_iarchive.hpp> #include <boost/serialization/vector.hpp> #include <boost/serialization/map.hpp> #include <boost/serialization/string.hpp> #include <boost/serialization/nvp.hpp> using namespace std; using namespace boost::filesystem; class DistributionTable { private: friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & BOOST_SERIALIZATION_NVP(neTable); ar & BOOST_SERIALIZATION_NVP(mappedTable); } vector<map<string,map<string,int> > > neTable; vector<map<string,map<string,int> > > mappedTable; public: DistributionTable(){}; void saveTable(const DistributionTable &t, string filename); void loadTable(DistributionTable &t, string filename); }; void DistributionTable::saveTable(const DistributionTable &t, string filename) { ofstream ofs(filename.c_str()); boost::archive::xml_oarchive oa(ofs); oa <<BOOST_SERIALIZATION_NVP(t); } void DistributionTable::loadTable(DistributionTable &t, string filename) { ifstream ifs(filename.c_str()); boost::archive::xml_iarchive ia(ifs); ia >>BOOST_SERIALIZATION_NVP(t); }
I suspect the problem is the following: ofstream ofs(filename.c_str()); boost::archive::xml_oarchive oa(ofs); oa <<BOOST_SERIALIZATION_NVP(t); here the archive is being destroyed while there still might exist something in the output stream buffer. Try this: ofstream ofs(filename.c_str()); boost::archive::xml_oarchive oa(ofs); ofs.flush(); oa <<BOOST_SERIALIZATION_NVP(t); Robert Ramey
participants (2)
-
Robert Ramey
-
Thomas D'Silva