Boost logo

Boost Users :

From: Mahesh Venkitachalam (mkvenkit.vc_at_[hidden])
Date: 2007-11-26 11:47:30


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_null<OStream::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<wchar_t>());
    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<wchar_t>());
    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 <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_woarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/binary_wiarchive.hpp>
#include <boost/serialization/base_object.hpp>
#include <boost/archive/codecvt_null.hpp>
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


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