2017-12-12 19 views
1

cereal, C++ 직렬화 라이브러리로 대형 벡터를 직렬화하고 싶습니다.대형 벡터 벡터 C++ 시리얼 비 직렬화 문제

예외가 + 표준 : to_string (크기) + "읽지 못했습니다"그렇게하려고하면 "입력 스트림로부터 바이트를! 읽기"+ 표준 : 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

당신의 코드가 리눅스에 나를 위해 잘 작동합니다. 소스 코드는 다음과 같다 내가으로 VisualStudio 2017

을 사용하고 ? 이것에 대한 좋은 해결책을 알고 있나요, 그래서 그것을 생각 Windows에서 텍스트와 바이너리 처리의 차이점과 관련이 있습니다. 입력 스트림을 구성 할 때 std::ios::binary을 전달합니다. 또한 std::fstream이 아닌 std::ifstream으로 구성하십시오.

이것은 Windows가 serializer를 혼동시키는 유니 코드 바이트 순서 표시를 예상하거나 추가하는 것과 관련이 있다고 생각합니다.

+0

의견을 보내 주셔서 감사합니다. 나는이 문제를 해결할 수 있었다. 바이트 순서를 올바르게 알아야합니다. – ric