Boost logo

Boost Users :

Subject: [Boost-users] boost::serialize stream error
From: ppatel_at_[hidden]
Date: 2009-02-11 16:18:52


Hi all,

I am having following problem while reading the data out of the file using one of the sample program provided in boost serialization example online. I am trying to read the file having multiple gps_position objects but getting following exception. Can someone please let me know what might be the problem here. Let me know if you need any more information:

#include <fstream>
#include <string>

// include headers that implement a archive in simple text format
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/list.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/serialization/base_object.hpp>

/////////////////////////////////////////////////////////////
// gps coordinate
//
// illustrates serialization for a simple type
//
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;
        ar & day;
    }

public:
    int degrees;
    int minutes;
    float seconds;
    std::string day;
    gps_position(){};
    gps_position(int d, int m, float s, std::string inDay) :
        degrees(d), minutes(m), seconds(s), day(inDay)
    {}
};

int main() {
    // create and open a character archive for output
    std::ofstream ofs("filename");
    // create class instance
    const gps_position g(35, 59, 24.567f, "Monday_Pointer");
    const gps_position g1(45, 69, 34.567f, "Tuesday_Pointer");
    const gps_position * g2 = new gps_position(1,2,3.3, "WEDNESDAY_PTR");
    {
        boost::archive::text_oarchive oa(ofs);
        oa << g;
        oa << g1;
        oa << (*g2);
    }
    {
        /**gps_position g;
        std::vector<gps_position> vec;
        std::ifstream ifs("filename");
        boost::archive::text_iarchive ia(ifs);
        while (!ifs.eof()) {
            std::cout << " ++++++++++++++++ " << std::endl;
            ia >> g;
            std::cout << g.degrees << " : "
                 << g.minutes << " : "
                 << g.seconds << " : "
                 << g.day <<std::endl;
            std::cout << " **************** " << std::endl;
        }*/
    }
    {
        // Using boost vector
        std::vector<gps_position> gps;
        std::ifstream ifs("filename");
        boost::archive::text_iarchive ia(ifs);
        ia >> gps;
        for (int i = 0; i < gps.size(); ++i) {
            std::cout << gps[i].degrees << " : "
                 << gps[i].minutes << " : "
                 << gps[i].seconds << " : "
                 << gps[i].day <<std::endl;
        }
    }
    return 0;
}

// File data that is being read
$ cat filename
22 serialization::archive 4 0 0 35 59 24.566999 14 Monday_Pointer 45 69 34.567001 15 Tuesday_Pointer 1 2 3.3 13 WEDNESDAY_PTR

// Output of program with while loop, don't know why eof is not being detected after reading three gps position objects
++++++++++++++++
35 : 59 : 24.567 : Monday_Pointer
 ****************
 ++++++++++++++++
45 : 69 : 34.567 : Tuesday_Pointer
 ****************
 ++++++++++++++++
1 : 2 : 3.3 : WEDNESDAY_PTR
 ****************
 ++++++++++++++++
terminate called after throwing an instance of 'boost::archive::archive_exception'
  what(): stream error
Aborted (core dumped)

//Output of program with using vector
terminate called after throwing an instance of 'boost::archive::archive_exception'
  what(): stream error
Aborted (core dumped)

Thanks
Priyank



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