2017-12-12 25 views
1

cereal、C++シリアライゼーションライブラリで大規模なベクトルをシリアル化することを望みます。大規模なベクトルでC++シリアル非直列化の問題

例外が+のstd :: to_stringに(サイズ)+ "の読み込みに失敗しました"、ということやろうとした場合、 "入力ストリームからバイトを!読む" +のstd :: to_stringに(READSIZEを)しかし、」スローされます。

は、誰もが

#include <iostream> 
#include <fstream> 
#include "include\cereal\cereal.hpp" 
#include "include\cereal\archives\binary.hpp" 
#include "include\cereal\types\vector.hpp" 
#include "include\cereal\types\string.hpp" 

void show(std::vector<int> v) { 
    for (auto i : v)std::cout << i << ","; 
    std::cout << std::endl; 
} 

int main(void) { 

    const std::string file_name = "out.cereal"; 

    { 
     std::vector<int> src; 
     // const int STOP = 10; //OK 
     const int STOP = 1000; // NG 

     for (int i = 0; i < STOP; i++)src.push_back(i); 
     std::cout << "src:" << std::endl; 
     show(src); 

     std::ofstream ofs(file_name, std::ios::binary); 
     cereal::BinaryOutputArchive archive(ofs); 
     archive(src); 
    } 

    { 
     std::vector<int> dst; 
     std::fstream fs(file_name); 
     cereal::BinaryInputArchive iarchive(fs); 
     iarchive(dst); 
     std::cout << "dst:" << std::endl; 
     show(dst); 
    } 

#ifdef _MSC_VER 
    system("pause"); 
#endif 
    return 0; 
} 

答えて

1

あなたのコードは、Linuxで私のために正常に動作します。ソースコードは以下のようになり、私はVisualStudioを2017年

を使用してい ?このための良い解決策を知っていたので、私はそれを考えますWindowsでのテキスト処理とバイナリ処理の違いに関係しています。入力ストリームを構築するときにstd::ios::binaryを渡します。また、std::fstreamではなくstd::ifstreamとしてください。

これは、シリアライザを混乱させているWindowsのUnicodeバイトオーダーマークを予期している(または追加する)ことと関係していると思います。

+0

ありがとうございます。私はこの問題を解決することができました。バイトオーダーを正しく理解する必要があります。 – ric

関連する問題