boost :: archive :: binary_oarchive =程序崩溃? [英] boost::archive::binary_oarchive = program crash?

查看:1429
本文介绍了boost :: archive :: binary_oarchive =程序崩溃?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题使用 boost :: archive :: binary_oarchive 。当执行程序时,我得到程序崩溃时实例化 ia>> boost :: serialization :: make_binary_object(buffer,size)
使用 boost :: archive :: text_oarchive 它工作...

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

提前感谢!

编辑:
这是它的工作原理。

This is how it works.

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


推荐答案

:)

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

现在可以完美工作了!

这篇关于boost :: archive :: binary_oarchive =程序崩溃?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆