[Serialization] Need help with wide character i/o
Hello, I am looking for some help in following instructions to create a wide character archive that I found from the boost serialization documentation: Actually the reason I am on this path is because my code is crashing if I try to do this in a normal way. (please see my previous post [Serialization] Strange crash with binary_iarchive)
To produce wide character text output (i.e. 16 bit characters on Win32 systems), do the following.
Open a wide character stream. Alter the stream locale to use boost::archive::codecvt_nullOStream::char_type Create the archive with the flag no_codecvt. Naturally, the input process has to be symmetrical.
My attempt (below) crashed when the archive is read back - what am I
doing wrong?
std::wofstream bofs("gps.bin", std::ios::binary);
std::locale loc(bofs.getloc(), new boost::archive::codecvt_null
Wide character i/o doesn't make sense for binary i/o. I know its in there but its not tested for that reason. here are a couple of suggestions. a) modify your small example or use the demo that came as part of the library to be sure that things work. Boost testing cannot test every possible combination of local settings. So I would recommend running the whole test suite on any library I acquire - including boost. b) assuming that enough demo's and tests run so you have confidence that your application should be possible, make a small series of tests modeled on the tests of the serialization libary which tests your serialization code. bottom level tests first, then tests which tests composition of more primitive types. Also, test these on various archive types. I know this sounds tedious and time consuming. But I think you'll find that its not that bad. OK maybe its a little tedious, but its not nearly as frustrating and just trying stuff until it works. (which is what I normally do). And its absolutly guarenteed to isolate the problem. For an added bonus, you keep the test program around so that they can be re-run everytime you make any modifications to any of the serialized classes. This will avoid - for free - the accidental introduction of a bug in to a binary archive. This is an excruciatingly difficult bug to find in a large program. c) Another trick is to test with the xml_archives. This checks that save/load functions are in sync by comparing tags. Robert Ramey Mahesh Venkitachalam wrote:
Hello,
I am looking for some help in following instructions to create a wide character archive that I found from the boost serialization documentation:
Actually the reason I am on this path is because my code is crashing if I try to do this in a normal way. (please see my previous post [Serialization] Strange crash with binary_iarchive)
To produce wide character text output (i.e. 16 bit characters on Win32 systems), do the following.
Open a wide character stream. Alter the stream locale to use boost::archive::codecvt_nullOStream::char_type Create the archive with the flag no_codecvt. Naturally, the input process has to be symmetrical.
My attempt (below) crashed when the archive is read back - what am I doing wrong?
std::wofstream bofs("gps.bin", std::ios::binary); std::locale loc(bofs.getloc(), new boost::archive::codecvt_null
()); bofs.imbue(loc); // create class instance const gps_position g2(36, 60, 24.567f); boost::archive::binary_woarchive oa(bofs, boost::archive::no_codecvt); oa << g2; bofs.close();
cout << "reading archive..." << endl; gps_position newg; // create and open an archive for input std::wifstream ifs("gps.bin", std::ios::binary); std::locale loc2(ifs.getloc(), new boost::archive::codecvt_null
()); ifs.imbue(loc2); // CRASHES NEXT LINE boost::archive::binary_wiarchive ia(ifs, boost::archive::no_codecvt); // read class state from archive ia >> newg; The serialized class is below:
#include <string> #include <vector> #include <fstream> #include
#include #include #include #include #include using namespace std; class gps_position { private: friend class boost::serialization::access; // When the class Archive corresponds to an output archive, the // & operator is defined similar to <<. Likewise, when the class Archive // is a type of input archive the & operator is defined similar to >>. template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & degrees; ar & minutes; ar & seconds; } int degrees; int minutes; float seconds; double* data; public: gps_position(){}; gps_position(int d, int m, float s) : degrees(d), minutes(m), seconds(s) {} };
Thanks
Mahesh
participants (2)
-
Mahesh Venkitachalam
-
Robert Ramey