2

I have a problem using boost::archive::binary_oarchive. When executing the program I get a program crash when instantiating the ia >> boost::serialization::make_binary_object(buffer, size). With boost::archive::text_oarchive it works...

#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/serialization/string.hpp>
#include <boost/serialization/binary_object.hpp>
#include <iostream>
#include <fstream>
using namespace std;
void save()
{
    size_t size = 0;     
    std::ifstream infile("any_file.png", std::ios::in | std::ios::binary | std::ios::ate);
    if (infile.is_open())
    {
        size = infile.tellg();
        char *buffer = new char[size];
        infile.seekg(0, ios::beg);
        infile.read(buffer, size);
        infile.close();

        std::ofstream file("archiv.bin");
        boost::archive::binary_oarchive oa(file);
        oa << size;
        oa << boost::serialization::make_binary_object(buffer, size);
        file.close();

        delete [] buffer;
    }
}

void load()
{
    size_t size = 0;
    std::ifstream file("archiv.bin");
    boost::archive::binary_iarchive ia(file);

    ia >> size;
    char *buffer = new char[size];
    ia >> boost::serialization::make_binary_object(buffer, size);  //program crash
    file.close();

    ofstream outfile("any_file_out.png", ios::out | ios::binary); 
    for(size_t i = 0; i < size; i++)
    {
        outfile << buffer[i];
    }
    outfile.close();
    delete [] buffer;
}

int main()
{
    save();
    load();
    return 0;
}

Thank you in advance!

Edit: This is how it works.

...
std::ofstream file("archiv.bin", ios_base::binary);
...
std::ifstream file("archiv.bin", ios_base::binary);
...
Chris Barlow
  • 3,274
  • 4
  • 31
  • 52
C Freak
  • 31
  • 5
  • WorksForMe™, checked the md5sums of `any_file.png` and `any_file_out.png`. I'm pretty sure it is a version conflict betwen the libs/headers you compile/link against and the version loaded at runtime. – sehe Mar 04 '12 at 21:23

1 Answers1

1

The solution presents itself :)

...
std::ofstream file("archiv.bin", ios_base::binary);
...
std::ifstream file("archiv.bin", ios_base::binary);
...

Works now perfectly!

C Freak
  • 31
  • 5