Mozart
Mozart

Reputation: 97

Boost serialization example error

I read the example and I try to run the following code: http://www.boost.org/doc/libs/1_58_0/libs/serialization/doc/index.html

#include <fstream>

// include headers that implement a archive in simple text format
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.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;
    }
    int degrees;
    int minutes;
    float seconds;
public:

    gps_position() {
    };

    gps_position(int d, int m, float s) :
    degrees(d), minutes(m), seconds(s) {
    }
};

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);

    // save data to archive
    {
        boost::archive::text_oarchive oa(ofs);
        // write class instance to archive
        oa << g;
        // archive and stream closed when destructors are called
    }

    // ... some time later restore the class instance to its orginal state
    gps_position newg;
    {
        // create and open an archive for input
        std::ifstream ifs("filename");
        boost::archive::text_iarchive ia(ifs);
        // read class state from archive
        ia >> newg;
        // archive and stream closed when destructors are called
    }
    return 0;
}

And I get the error:

terminate called after throwing an instance of 'boost::archive::archive_exception'
  what():  output stream error

RUN FINISHED; Aborted; core dumped; real time: 100ms; user: 0ms; system: 0ms

Upvotes: 0

Views: 1163

Answers (1)

Spacemoose
Spacemoose

Reputation: 4016

I just copy-pasted the code, compiled, ran, no-problem. Based on this and your error message, I'm guessing you have a system problem. Probably something is preventing you from writing your file. The most likely culprit is a permission error. If you were writing a large file I might guess disk full, but it seems unlikely you're that full up.

Try running the executable somewhere else, or hard coding a path to somewhere you have write permissions. Try a simple hellow world with ofstream to see if you can create and write a file.

Upvotes: 1

Related Questions