Reputation: 467
In below code snippet , I am storing 2 objects in 'filename' file but surprised to see first object value retrieval 2 times during de-serialization. Apart from this i would like to store and retrieve multiple class objects in same file . currently i am able to store objects in file but not able to retrieve.
can anybody through some light on this?
#include <fstream>
#include <iostream>
// include headers that implement a archive in simple text format
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
using namespace std;
/////////////////////////////////////////////////////////////
// object_model
//
// illustrates serialization for a simple type
//
class object_model
{
private:
friend class boost::serialization::access;
// When the class Archive corresponds to an output archive, the
// & operator is defined similar to <<. Likewise, when the class Archive
// is a type of input archive the & operator is defined similar to >>.
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & classval;
ar & property;
}
string classval;
int property;
public:
object_model(){};
object_model(string d, int p) :
classval(d), property(p)
{}
string getClassval()
{
return classval;
}
int getproperty()
{
return property;
}
};
int main() {
// create and open a character archive for output
std::ofstream ofs("filename");
// create class instance
object_model g("lidar",10);
{
// save data to archive
boost::archive::text_oarchive oa(ofs);
// write class instance to archive
oa << g;
}
object_model g1("lidar1",20);
{
// save data to archive
boost::archive::text_oarchive oa(ofs);
// write class instance to archive
oa << g1;
}
// ... some time later restore the class instance to its orginal state
object_model newg;
{
// create and open an archive for input
std::ifstream ifs("filename");
boost::archive::text_iarchive ia(ifs);
// read class state from archive
ia >> newg;
cout<<newg.getClassval();
cout<<newg.getproperty();
cout<<"done"<<endl;
}
// archive and stream closed when destructors are called
object_model newg1;
{
// create and open an archive for input
std::ifstream ifs("filename");
boost::archive::text_iarchive ia(ifs);
// read class state from archive
ia >> newg1;
cout<<newg1.getClassval();
cout<<newg1.getproperty();
cout<<"done"<<endl;
}
return 0;
}
Upvotes: 2
Views: 322
Reputation: 852
while saving data to archive object you need to redirect both the object altogether .it works perfectly fine for me.
object_model g("lidar",10); object_model g1("lidar1",20);
{ // save data to archive
boost::archive::text_oarchive oa(ofs);
// write class instance to archive
oa << g<<g1;
}
// ... some time later restore the class instance to its orginal state
object_model newg;
object_model newg1;
{
// create and open an archive for input
std::ifstream ifs("filename");
boost::archive::text_iarchive ia(ifs);
// read class state from archive
ia >> newg>>newg1;
cout<<newg.getClassval();
cout<<newg.getproperty();
cout<<newg1.getClassval();
cout<<newg1.getproperty();
cout<<"done"<<endl;
}
Upvotes: 1