0
아래 예제를 하드 디스크에 간단한 바이너리 파일로 저장하고 싶습니다. 하지만 그물 주위에 나는 그것을 어떻게하는지 간단하고 깨끗한 예제를 찾지 못했습니다. 그래서 질문하고 있습니다 :하드 디스크에 boost :: serialization :: serialize'ble 구조체를 바이너리로 저장하십시오.
구조체를 이진 파일로 이진 파일로 저장하려면 아래의 코드를 어떻게 수정합니까?
#include <vector>
#include <string>
#include <bitset>
#include <boost/serialization/string.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/serialization/bitset.hpp>
template<size_t N>
struct Example
{
std::string id;
std::vector<std::bitset<N>> bits;
};
template<size_t N>
Example<N> make_example()
{
Example<N> example;
example.id = "some id";
example.bits.resize(100);
}
namespace boost
{
namespace serialization
{
template<typename Archive, size_t N>
void serialize (Archive & a
, Example<N> & e
, const unsigned int version)
{
a & e.id;
a & e.bits;
}
}
}
int main()
{
auto example = make_example<256>();
std::ofstream ofs("filename", std::ios::binary);
boost::archive::binary_oarchive oa(ofs);
oa << example; // shouldn't use << as oa writes a text archive
}
실제로 이전 게시물에서 일부 복사/붙여 넣기 오류가있었습니다. 내가 제안한대로 편집했습니다.프로그램을 실행할 때 genereated 파일 _filename_은 다음과 같은 텍스트 내용을 표시합니다 (~ 25600자를 예상 한대로). '^ @^D^H^D^H^A^@^@^@ @^@ @ @^@^@^^ G^@^@^@^@^@^@^어떤 아이디^@^^ @^@ @^@^^ @ @^@^@^@ @ @^@^@^@^@^@^@^@^@^@^@^@^^ @^@^@^@^@^@^@^0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000^@^@^@^@^@^0 '@ – user1587451
그럼에도 불구하고 여전히 정상적으로 작동하지 않습니다. 파일 크기는 약 26K이지만 256 * 100bit = 3200byte = 3,125K입니다. 파일 크기가 예상보다 8 배 더 큽니다. – user1587451
@ user1587451 실제로 이진 압축 파일입니다. text_oarchive로 변경하고 해당 헤더를 포함하여 차이점을 확인하십시오. 비트 세트로 인해 차이가 조금 있지만 텍스트 보관소에서는 텍스트 메타 데이터가 더 많습니다. – aichao