2012-03-04 163 views
2

我使用boost::archive::binary_oarchive时出现问题。在执行程序时,我在实例化ia >> boost::serialization::make_binary_object(buffer, size)时遇到程序崩溃。 随着boost::archive::text_oarchive它的工作原理...boost :: archive :: binary_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; 
} 

预先感谢您!

编辑: 这是如何工作的。

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

WorksForMe™,检查了'any_file.png'和'any_file_out.png'的md5sums。我很确定这是编译/链接的libs /头文件与运行时加载的版本之间的版本冲突。 – sehe 2012-03-04 21:23:55

回答

1

的解决方案提出了自己:)

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

现在的作品完美!