2012-03-04 165 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); 
... 

現在的作品完美!