[serialization] stream error when reading simple tree structure from a binary archive

Hi,
I tried to save a tree structure out to an archive and then read it back. If
I use a text archiver, everything works fine; If I use a binary archiver, an
exception of "stream error" is thrown. I managed to shorten my code as
following.
Can anyone give some advice? I'm using vs2008 and boost 1.42.0.
Thanks.
///////////////////////////////////////////////////////////////////////
#define _SCL_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#include <fstream>
#include

CurieCat wrote:
Hi, I tried to save a tree structure out to an archive and then read it back. If I use a text archiver, everything works fine; If I use a binary archiver, an exception of "stream error" is thrown. I managed to shorten my code as
following. Can anyone give some advice? I'm using vs2008 and boost
1.42.0. Thanks.
[snip]
const Node* rootnode = root ;
Node* newroot = NULL; std::ofstream ofs("filename"); {
boost::archive::binary_oarchive oa(ofs); oa << rootnode; } ofs.close();
std::ifstream ifs("filename"); { boost::archive::binary_iarchive ia(ifs);
ia >> newroot; } ifs.close();
return 0; }
The first obvious thing I can notice with the code above is that it's not opening the files in binary mode. Try std::ofstream ofs("filename", std::ios_base::binary); and std::ifstream ifs("filename", std::ios_base::binary); HTH, Gevorg

CurieCat wrote:
const Node* rootnode = root ; Node* newroot = NULL; std::ofstream ofs("filename", std::ios::binary); { boost::archive::binary_oarchive oa(ofs); oa << rootnode; } ofs.close(); std::ifstream ifs("filename", std::ios::binary); { boost::archive::binary_iarchive ia(ifs); ia >> newroot; } ifs.close(); return 0; }
try adding std::ios::binary in the right place. Robert Ramey
participants (3)
-
CurieCat
-
Gevorg Voskanyan
-
Robert Ramey