c ++ - boost :: arkiv :: binary\_oarchive=programkrasch?

Indlæg af Hanne Mølgaard Plasc

Problem



Jeg har et problem ved at bruge boost::archive::binary\_oarchive. Når jeg udfører programmet, får jeg et programkrasj, når jeg instanserer ia >> boost::serialization::make\_binary\_object(buffer, size).
Med boost::archive::text\_oarchive fungerer det ...


#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;
}


Tak på forhånd!


Redigere:
Sådan virker det.


...
std::ofstream file("archiv.bin", ios\_base::binary);
...
std::ifstream file("archiv.bin", ios\_base::binary);
...

Bedste reference


Løsningen præsenterer sig :)


...
std::ofstream file("archiv.bin", ios\_base::binary);
...
std::ifstream file("archiv.bin", ios\_base::binary);
...


Fungerer nu perfekt!