2

Just starting to familiarize myself with the Boost serialization library. I'm stumped on what appears to be a data-dependent failure:

The following code fails with input stream error

#include <cassert>
#include <vector>
#include <iostream>
#include <algorithm>
#include "boost/serialization/vector.hpp"
#include "boost/archive/text_iarchive.hpp"
#include "boost/archive/text_oarchive.hpp"
#include "boost/archive/binary_iarchive.hpp"
#include "boost/archive/binary_oarchive.hpp"

int main (void) {

    std::vector<int> v1(100);
    std::generate(v1.begin(), v1.end(), &std::rand);

    {
        std::ofstream ofs("test.out");
        boost::archive::binary_oarchive oa(ofs);
        oa << v1;
    }

    {
        std::vector<int> v2;
        std::ifstream ifs("test.out");
        boost::archive::binary_iarchive ia(ifs);
        ia >> v2;
        assert(v1 == v2);
    }

    return 0;
}

If I use boost::archive::text_[i/o]archive, the code passes.

If I comment out the std::generate line (still using binary_[i/o]archive), the code passes.

On the surface, this is almost impossible to believe. More likely, I am missing something obvious.

Lastly, using 1.53.

0

1 Answer 1

3

It's possible that your fstream is converting 0x0a bytes that appear in your binary stream to your system line ending sequence which is not 0x0a. Try opening your files with the std::ios::binary mode, e.g.

    std::ofstream ofs("test.out", std::ios::out | std::ios::binary);

and

    std::ifstream ifs("test.out", std::ios::in | std::ios::binary);
Sign up to request clarification or add additional context in comments.

1 Comment

my bet is on this too

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.