[serialization] binary archives and text/binary streams

There seems to be a problem on win32 where if you use a stream opened in text mode for a binary archive, you get errors. The following code asserts because 'd' appears to have a random value on the way back in. If you open both streams in binary mode then the code runs fine. XML archives don't have the same problem. I realise you shouldn't use a binary archive with a text stream, but is there anyway to catch this at compile or run time? Otherwise you just end up with a corrupted archive? Thanks Russell double d = -6.7; { ofstream ofs("C:\\test.dat"); archive::binary_oarchive oa(ofs); oa & d; } d = 0.0; { ifstream ifs("C:\\test.dat"); archive::binary_iarchive ia(ifs); ia & d; } assert(std::abs(d + 6.7) < 0.0001);

"Russell Hind" <rh_gmane@mac.com> wrote in message news:cll9ma$fi4$1@sea.gmane.org...
There seems to be a problem on win32 where if you use a stream opened in text mode for a binary archive, you get errors. The following code asserts because 'd' appears to have a random value on the way back in.
If you open both streams in binary mode then the code runs fine. XML archives don't have the same problem.
I realise you shouldn't use a binary archive with a text stream, but is there anyway to catch this at compile or run time? Otherwise you just end up with a corrupted archive?
I looked hard for a way to determine whether a previously open stream was opened in binary or text mode. I was unable to find a way to do this - portable or otherwise. The assertion was the best I could do. Robert Ramey

"Russell Hind" wrote:
There seems to be a problem on win32 where if you use a stream opened in text mode for a binary archive, you get errors.
I realise you shouldn't use a binary archive with a text stream, but is there anyway to catch this at compile or run time? Otherwise you just end up with a corrupted archive?
Probably no way to find stream is text or binary. Maybe if serialization library inserts '\n' at the opening and asks for tellp() to check how many bytes are in. /Pavel
participants (3)
-
Pavel Vozenilek
-
Robert Ramey
-
Russell Hind