2013-07-24 18 views
4

부스트 필터링 _ 스트림에 대한 몇 가지 기본적인 질문. 나는 지금은 압축 된 ZIP 파일로 출력 부스트 filtering_stream을 사용하고 싶습니다 :: 표준의 매개 변수를 사용 기능 수십를 &std :: ofstream을 boost 필터링 _ 스트림으로 대체하기위한 Param 구문

void foo(std::ofstream& outStream) 
{ 
    // lots of operations, like this: 
    outStream << "various bits of text"; 
} 

void StreamSomeTextToFile(char* fileName) 
{ 
    ofstream myFileStream(fileName, ios::out | ios::app | ios::binary); 
    foo(myFileStream); 
    myFileStream.close(); 
} 

을 ofstream. 일반적으로 언급 된 필터링 및 압축 해제를위한 filtering_streams 테스트 코드는 컴파일, 링크 및 완벽하게 작동합니다.

void StreamSomeCompressedTextToFile(char* fileName) 
{ 
    ofstream myFileStream(destPath, std::ios_base::out | std::ios_base::app | std::ios_base::binary); 
    boost::iostreams::filtering_streambuf<boost::iostreams::output> myCompressedFileStream; 
    myCompressedFileStream.push(boost::iostreams::zlib_compressor()); 
    myCompressedFileStream.push(myFileStream); 

    foo(myCompressedFileStream); // I can't just pass myCompressedFileStream to foo(std::ofstream&), right? 
    myFileStream.close(); 
} 

세 가지 질문 : 나는 filtering_stream을 대체하고 싶습니다

1) 마십시오 이전에 표준 받아 내 모든 기능 :: ofstream 지금 형 부스트의 매개 변수를 받아 들일 수 & outStream 필요 :: IOSTREAMS :: filtering_streambuf &? 또는 적절한 매개 변수 유형이있어 수많은 ("foo") 함수가 어느 유형의 스트림 유형에서도 작동 할 수 있습니까? 내 간단한 테스트의 경우

2), 나는 filtering_streambuf와 스트림 연산자 구문을 사용 할 수 없습니다 :

myCompressedFileStream << "some text"; 

이이 오류가 발생하지 : '연산자 < <'에 대한 일치를. , 나는 내가 후에 파일 "hello.z"을 찾을 수 있음을 혼동했다

error: 'class boost::iostreams::filtering_streambuf<boost::iostreams::output, char, std::char_traits<char>, std::allocator<char>, boost::iostreams::public_>' has no member named 'write '

3) 일반적인 테스트 케이스 예제 코드 (아래)에서 : 나는 유사 쓰기와 오류를() 컴파일했다 만들어졌습니다. 압축 풀기 코드 (아래에도 있음)가이를 분명하게 참조합니다 - 어디에서 찾을 수 있습니까? 참고 : 위치가 마지막으로 발견되었다 :가/라이브러리/환경 설정했다/

void pack() 
{    
    std::ofstream file("hello.z", std::ios_base::out | std::ios_base::binary); 

    boost::iostreams::filtering_streambuf<boost::iostreams::output> out; 
    out.push(boost::iostreams::zlib_compressor()); 
    out.push(file);  
    char data[5] = {'a', 'b', 'c', 'd', 'e'};  
    boost::iostreams::copy(boost::iostreams::basic_array_source<char>(data, sizeof(data)), out); 
    file.close(); 
} 

void unpack() 
{ 
    std::fstream file("hello.z", std::ios_base::in | std::ios_base::binary); 
    boost::iostreams::filtering_streambuf<boost::iostreams::input> in; 
    in.push(boost::iostreams::zlib_decompressor()); 
    in.push(file); 
    boost::iostreams::copy(in, std::cout); 
} 

BTW : 엑스 코드 3.2.6, GNU 4.0, OS X 10.6.8

답변

3

위해 질문을 촬영 :

1 : Stream 버퍼 객체 (예 : boost :: iostream :: filtering_streambuf 또는 std :: streambuf)는 스트림 객체 (예 : std :: ostream 또는 boost 구현)와 호환 할 수 없습니다. 즉, "myCompressedFileStream"과 같은 streambuf 객체를 ostream 객체의 생성자에 전달할 수 있습니다 (this boost iostream tutorial은 예를 들어 알맞은 설명을 제공함). 부스트의 streambufs는 표준 라이브러리에있는 것들과 호환되기 때문에 std :: ostream/ofstream 참조를 받아들이는 함수를 변경할 필요가 없습니다. streambufs를 스트림으로 전달할 수 없습니다.

2 : 삽입 연산자는 streambufs가 아니라 streams에 대해 정의됩니다.

3 : 일반적으로 디렉토리 이름이없는 파일은 실행 파일의 디렉토리에 생성됩니다. 즉, 필자는 Finder가 비 - Finder 프로세스에 의해 업데이트되거나 생성 된 파일을 반영하는 속도가 다소 느린 경우가 있음을 발견했습니다. ls를 사용하는 터미널에서 이러한 문제를 경험하지 못했습니다. 그래도 그것이 당신의 문제와 관련이 있는지는 알 수 없습니다.

+0

위대한, 그리고 매우 도움이 - 아직 거기에 대해 아직도 혼란 스러워요 : filtering_streambuf에서 ostream 개체를 구축 : filtering_streambuf (myCompressedFileStream) 필요합니다 ofstream 정의 된 첫 번째 (myFileStream) 푸시 할 수 있도록 . myCompressedFileStream에서 ostream을 생성 한 다음 새로 생성 된 ostream으로 출력하면 파일은 비어 있습니다. – SMGreenfield

2

성공!

폴 Schellin (위)과 부스트 사용자에 대한 몇 가지의 힌트의 조합은이 질문에 대해 결과

: 프레데릭는 output_file에 [filtering_ostream] 파괴 될 때까지 아무것도 [S] 일이 없다 "고 지적했다

1) 부스트 사용 그래서 {}로 묶으십시오. 이것은 필자의 누락 된 부분이었습니다. 왜냐하면 나는 filtering_streambuf가 파괴되기 전에 ofstream에 file.close()를 시도했기 때문입니다. 그것이 파일이 비어있는 이유입니다.

이 다시 읽는 문서가 공개 :이 상태

"By default, if the Device at the end of the chain is popped 
    or if the filtering_stream is complete when it is destroyed, 
    all the filters and devices in the chain are closed using the 
    function close. This behavior can be modified using the member 
    function set_auto_close" 

거기 filtering_stream의 스택에서 "팝업"압축기() 또는 ofstream (파일) 할 필요가 없으며, 가까운 호출하는 것입니다() . 그냥 filtering_stream 객체를 소멸 시키면 모든 것이 기록되고 정리됩니다. 애매한 세부 사항과 기대되는 것과 상반되는 내용.

3) 부스트 사용자 Holger Gerth는 filtering_stream을 사용할 수 있었을 때 왜 filtering_streambuf를 사용했는지 의문을 제기했습니다. 사실, 필자의 실험에서는 filtering_stream에서 ostream (다른 함수로 전달해야하는)을 구성 할 수 없으며 필요한 ostream 대신 filtering_stream을 전달할 수도 없었습니다.

filtering_streambuf 대 filtering_stream에 대한 여러 기사를 읽은 후에도 filtering_streambuf에서 ostream을 생성하는 데 filtering_stream을 사용하는 방법과 이유 (이유는 무엇인가)가 여전히 신비 스럽습니다.

SO, 요약하자면 :

1)는 별도의 filtering_streambuf ostream에 구축하고 foo는 것을 합격() 또는 스트림 삽입 연산자 (즉 < <)이다.

2) myFileStream.close()를 호출하지 마십시오.