#include #include #include #include #include #include #include #include struct S { double d; #define SIZE 100 char c[SIZE]; char* x; BOOST_SERIALIZATION_SPLIT_MEMBER() template void save(Archive& ar, const unsigned int version) const { ar & this->d; for (int i = 0; i < SIZE; ++i) ar & this->c[i]; // serializing -- write number of chars to archive int len = strlen(this->x); ar & len; for (int i = 0; i < len; ++i) ar & this->x[i]; } template void load(Archive& ar, const unsigned int version) { ar & this->d; for (int i = 0; i < SIZE; ++i) ar & this->c[i]; // deserializing -- read number of chars from archive and allocate // a buffer large enough into `x` int len; ar & len; this->x = new char[len + 1]; for (int i = 0; i < len; ++i) ar & this->x[i]; } }; int main(int argc, char** argv) { S s; s.d = 42.0; strcpy(s.c, "hello"); s.x = strdup("world"); // write archive std::ofstream ofs("test.txt"); boost::archive::text_oarchive oa(ofs); oa << s; ofs.close(); // read it back std::ifstream ifs("test.txt"); boost::archive::text_iarchive ia(ifs); S s2; s2.x = NULL; ia >> s2; std::cout << "S.d=" << s2.d << std::endl << "S.c=" << s2.c << std::endl << "S.x=" << s2.x << std::endl; return 0; }