boost::iostreams
에 관한 다음 질문이 있습니다. 누군가 필터 작성에 익숙하다면 조언/도움에 감사드립니다.boost :: iostream을 std :: ios :: binary와 비슷한 모드에서 작동시키는 방법?
나는 boost::iostream::filtering_stream
와 함께 작동하는 한 쌍의 다중 필터를 작성 중이며, 데이터 압축기와 압축 풀기 기가 있습니다. 저는 컴프레서를 작성하는 것으로 시작하여 lz 패밀리에서 일부 알고리즘을 선택했으며 현재 압축 해제기에 대한 작업을하고 있습니다.
몇 마디로 말해, 압축기는 데이터를 패킷으로 분할하여 별도로 인코딩 한 다음 내 파일로 플러시합니다. 내 파일에서 데이터를 복원해야 할 때
, 내가하는 전체 포장 블록을 읽고 그것을 bufferize, 압축을 풀고 만 다음 요구 된 바이트 수를 제공해야 (프로그래밍 측면에서하는 read(byte_count)
요청을 수신) . 나는이 논리를 구현했습니다,하지만 지금 내가 와 함께 다음과 같은 문제 사투를 벌인거야 :
내 데이터를 포장 할 때, 어떤 기호는 출력 파일에 나타날 수 있습니다. 그리고 (hex 1A, char 26)
기호를 포함하는 파일을 읽을 때 문제가 발생했습니다. boost::iostreams::read(...., size)
을 사용합니다.
예를 들어 std::ifstream
을 사용하는 경우 std::ios::binary
모드로 설정하면이 기호를 간단히 읽을 수 있습니다.
boost::iostream
boost::iostream::read
루틴을 사용하여 char 시퀀스를 읽는 필터를 구현할 때 어떤 방법으로도 동일한 결과를 얻을 수 있습니까? 여기
일부 코드 : 바이너리로 파일을 읽기위한
// Compression
// -----------
filtering_ostream out;
out.push(my_compressor());
out.push(file_sink("file.out"));
// Compress the 'file.in' to 'file.out'
std::ifstream stream("file.in");
out << stream.rdbuf();
// Decompression
// -------------
filtering_istream in;
in.push(my_decompressor());
in.push(file_source("file.out"));
std::string res;
while (in) {
std::string t;
// My decompressor wants to retrieve the full block from input (say, 4096 bytes)
// but instead retrieves 150 bytes because meets '1A' char in the char sequence
// That obviously happens because file should be read as a binary one, but
// how do I state that?
std::getline(in, t); // <--------- The error happens here
res += t;
}
컴파일 가능한 테스트 케이스는 항상 텍스트의 벽을 이길 수 있습니다. – YeenFei
비슷한 문제가 발생했습니다 : http://stackoverflow.com/questions/224234/is-there-a-way-to-check-if-an-istream-was-open-in-binary-mode – Ferruccio