简体   繁体   中英

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

I have an object with several vector of other objects as members. I want to write this object to the file 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

您可以使用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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM