Hi Robert,

 

Thanks for your reply. Can you please check following code and see what I am doing wrong.

I am writing one gps_postiion object in the file and after  reading that object back from the file I think I should hit end of the file character.

Why there is extra “\n” character placed after writing the object?

 

 

Writing through serialization:

 

struct gps_position

{

    friend class boost::serialization::access;

    template<class Archive>

    void serialize(Archive & ar, const unsigned int version)

    {

        ar & degrees;

    }

public:

    int degrees;

};

BOOST_CLASS_TRACKING(gps_position, boost::serialization::track_never);

 

int main() {

    // create and open a character archive for output

    std::ofstream ofs("filename2");

    // create class instance

    gps_position g;

    g.degrees = 100;

    {

        boost::archive::text_oarchive oa(ofs);

        oa << g;

    }

    {

        gps_position g1;

        std::ifstream ifs("filename2");

        boost::archive::text_iarchive ia(ifs);

        ia >> g1;

        std::cout << " g1 data: " << g1.degrees << std::endl;

        while (!ifs.eof()) {

            std::cout << " EXTRA CHARACTERS REMAINING: " << ifs.get() << std::endl;

        }

    }

    return 0;

}

 

File name has new line character in this.

 

$ od -x filename2

0000000 3232 7320 7265 6169 696c 617a 6974 6e6f

0000020 3a3a 7261 6863 7669 2065 2034 2030 2030

0000040 3031 0a30

Output of this program:

 

$ ./boost_serialize

 g1 data: 100

 EXTRA CHARACTERS REMAINING: 10 (end line character)

 EXTRA CHARACTERS REMAINING: -1

 

 

Regular write (doesn’t have \n at the end):

 

#include <iostream>

#include <iomanip>

#include <fstream>

using namespace std;

int main() {

    ofstream outFile ("test.txt");

    int x = 100;

    outFile << x; outFile.close();

    return 0;

}

 

Output of this program does not have “\n” character at the end.

 

$ od -x test.txt

0000000 3031 0030

0000003

 

Thanks

Priyank

 

From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Robert Ramey
Sent: Thursday, February 12, 2009 1:01 AM
To: boost-users@lists.boost.org
Subject: Re: [Boost-users] boost::serialize stream error

 

The serialization library only reads what it wrote.  So I would never expect to read the eof.  And this is for a very good reason.  It permits serialized data to be embedded inside of other data.

 

Robert Ramey

<ppatel@efs-us.com> wrote in message news:810D860474553A4C884BBDA56703854DC05ACD0558@XEX.efs-us.com...

Hi,

 

I have checked some more on this and it seems there is one more character remaining after three objects has been read (0xa) and that is why eof() is not returning true. Can someone please let me know if they come across this problem or tell me what I am doing  incorrect here? Thanks.

 

// 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

 

// Y_PTR = Y= 59  _ = 5f, P = 50, T = 54, R = 52

// 5f59 5450 0a52

 

$ od -x filename

0000000 3232 7320 7265 6169 696c 617a 6974 6e6f

0000020 3a3a 7261 6863 7669 2065 2034 2030 2030

0000040 3533 3520 2039 3432 352e 3636 3939 2039

0000060 3431 4d20 6e6f 6164 5f79 6f50 6e69 6574

0000100 2072 3534 3620 2039 3433 352e 3736 3030

0000120 2031 3531 5420 6575 6473 7961 505f 696f

0000140 746e 7265 3120 3220 3320 332e 3120 2033

0000160 4557 4e44 5345 4144 5f59 5450 0a52

0000176

 

Follows is updated program:

 

while (!ifs.eof()) {

            std::cout << "BAD: " << (ifs.rdstate() & std::ios::badbit)

                        << "FAIL:" << (ifs.rdstate() & std::ios::failbit)

                        << "GOOD: " << (ifs.rdstate() & std::ios::goodbit)

                        << "EOF: " << (ifs.rdstate() & std::ios::eofbit)

                        << std::endl;

            std::cout << " ++++++++++++++++  " << std::endl;

            if (counter == 3) {

                while (!ifs.eof()) {

                    std::cout << ifs.get() << std::endl;

                }

                std::cout << "BAD: " << (ifs.rdstate() & std::ios::badbit)

                        << "FAIL:" << (ifs.rdstate() & std::ios::failbit)

                        << "GOOD: " << (ifs.rdstate() & std::ios::goodbit)

                        << "EOF: " << (ifs.rdstate() & std::ios::eofbit)

                        << std::endl;

                break;

            }

            ++counter;

            ia >> g;

            std::cout << g.degrees << " : "

                 << g.minutes << " : "

                 << g.seconds << " : "

                 << g.day <<std::endl;

            std::cout << " ****************  " << std::endl;

        }

 

ppatel@cinderella [~/MY_TEST/BOOST] $ ./boost_serialize BAD: 0FAIL:0GOOD: 0EOF: 0

 ++++++++++++++++

35 : 59 : 24.567 : Monday_Pointer

 ****************

BAD: 0FAIL:0GOOD: 0EOF: 0

 ++++++++++++++++

45 : 69 : 34.567 : Tuesday_Pointer

 ****************

BAD: 0FAIL:0GOOD: 0EOF: 0

 ++++++++++++++++

1 : 2 : 3.3 : WEDNESDAY_PTR

 ****************

BAD: 0FAIL:0GOOD: 0EOF: 0

 ++++++++++++++++

10

-1

 

Thanks

Priyank

 

From: Priyank Patel
Sent: Wednesday, February 11, 2009 3:19 PM
To: 'Boost-users@lists.boost.org'
Subject: boost::serialize stream error

 

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 mailing list
Boost-users@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users