C++ boost::archive::binary\u oarchive=程序崩溃?
我在使用C++ boost::archive::binary\u oarchive=程序崩溃?,c++,windows,serialization,boost,C++,Windows,Serialization,Boost,我在使用boost::archive::binary\u oarchive时遇到问题。在执行程序时,当实例化ia>>boost::serialization::make_binary_对象(缓冲区、大小)时,我会遇到程序崩溃。 使用boost::archive::text\u oarchive它可以工作 #include <boost/archive/binary_oarchive.hpp> #include <boost/archive/binary_iarchive.hpp
boost::archive::binary\u oarchive
时遇到问题。在执行程序时,当实例化ia>>boost::serialization::make_binary_对象(缓冲区、大小)
时,我会遇到程序崩溃。
使用boost::archive::text\u oarchive
它可以工作
#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;
}
解决方案出现了:)
现在可以完美地工作了 工作模板™, 检查了
any_file.png
和any_file_out.png
的MD5总和。我很确定编译/链接所针对的libs/头和运行时加载的版本之间存在版本冲突。
...
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);
...