Boost logo

Boost Users :

Subject: Re: [Boost-users] [serialization] stream error when reading simple tree structure from a binary archive
From: Gevorg Voskanyan (v_gevorg_at_[hidden])
Date: 2010-03-13 11:49:16


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


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net