开发者

Write and read a complex object to file in c++

开发者 https://www.devze.com 2023-02-03 00:57 出处:网络
I have an object with several vector of other objects as members. I want to write this object to the fi开发者_开发百科le all at once(in binary mode), instead of writing each members. How can I do that

I have an object with several vector of other objects as members. I want to write this object to the fi开发者_开发百科le all at once(in binary mode), instead of writing each members. How can I do that? in this case i want to write object "A" from class AccList(all of my data saved in "A"!) this is my ClassDiagram: http://askus.ir/ClassDiagram1.png


You could use boost::serialization


you can use boost::serialization for do that

you must add one method at your object

template<class Archive>
void serialize(Archive & ar,  const unsigned int version)
{
    ar & this->foo & this->bar;
}

this method define what member has to be stored

after, for save your object you must do:

std::ofstream ofs("filename");
boost::archive::binary_oarchive oa(ofs);
oa << data;

for read the object, it's the same way,

std::ifstream ifs("filename");
boost::archive::binary_iarchive ia(ifs);
ia >> data;

tutorial

0

精彩评论

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