
gongyiling wrote:
Hello, Everyone:
boost::serialization is a cool library. I use it in my project. I encounter a problem now. I serialized a array of objects to file as following:
std::ofstream ofs(filename, std::ios_base::binary); boost::binary_oarchive oar(ofs); oar << obj[0] << obj[1] << ... << obj[n-1];
Now I want to load the obj[i] (i>=0 && i
struct word_block { std::string word; template<typename Archive> void serialize(Archive & ar, const unsigned int file_version ) { ar & word; } };
int main(int argc, char* argv[]) { const char* filename = "words.idx"; word_block wb1, wb2, wb2_r;
std::ofstream ofs(filename, std::ios_base::binary); boost::archive::binary_oarchive oar(ofs); oar << wb1; size_t wb2_pos = ofs.tellp(); //record the position of wb2. oar << wb2; ofs.close();
std::ifstream ifs(filename, std::ios_base::binary); boost::archive::binary_iarchive iar(ifs); ifs.seekg(wb2_pos); //set file pointer to wb2. iar >> wb2_r; //failed with exception input_stream_error. return 0; }
Any idea is appreciated!
I can't see a way to do this without making non-trivial changes in the serialization library Robert Ramey