2011-01-17 2 views
0

파일의 압축을 풀고 해당 내용을 stringstream에 쓰려고합니다.압축되지 않은 filtering_istream을 문자열 스트림에 복사하려고하면 오류가 발생합니다.

그것은 경고없이 컴파일
string readGZipLog() { 
try { 
     using namespace boost::iostreams; 
     ifstream file(currentFile.c_str(), std::ios_base::in | std::ios_base::binary); 
     boost::iostreams::filtering_istream in; 
     in.push(gzip_decompressor()); 
     in.push(file); 
     std::stringstream strstream; 
     boost::iostreams::copy(in, strstream); 
     return strstream.str(); 
} catch (std::exception& e) { 
     cout << e.what() << endl; 
} 
} 

void writeGZipLog (char* data) { 
    try { 
     using namespace boost::iostreams; 
     std::ofstream file(currentFile.c_str(), std::ios_base::out | std::ios_base::binary); 
     boost::iostreams::filtering_ostream out; 
     out.push(gzip_compressor()); 
     out.push(file); 
     std::stringstream strstream; 
     strstream << data; 
     boost::iostreams::copy(strstream, data); 
    } catch (std::exception& e) { 
     cout << e.what() << endl; 
    } 
} 

(물론 오류의)하지만 함수를 실행하는 동안 readGZipLog() 충돌 :

이것은 내가 노력 코드는

gzip error 
./build: line 3: 22174 Segmentation fault  ./test 

./build 스크립트입니다 응용 프로그램을 컴파일하고 시작합니다. ./test 자동으로

c 파일에 싫증이났다 : 무언가가 들어 있지만을 사용하여 압축을 풀 수 없다. 그래서 압축이 제대로 작동했는지, 그리고 Boost에 의해 throw 된 gzip error과 관련이 있는지 확실하지 않습니다.

오류가 발생한 위치를 알려 줄 수 있습니까?

도움 주셔서 감사합니다.

답변

2

많은 연구 후에 나는 마침내 제대로 (드) 압축을 처리하는 방법을 방법을 발견하려고.

string readGZipLog() { 
    using namespace boost::iostreams; 
    using namespace std; 
    try { 
     ifstream file(currentFile.c_str(), ios_base::in | ios_base::binary); 
     boost::iostreams::filtering_istream in; 
     in.push(gzip_decompressor()); 
     in.push(file); 
     stringstream strstream; 
     boost::iostreams::copy(in, strstream); 
     return strstream.str(); 
    } catch (const gzip_error& exception) { 
     cout << "Boost Description of Error: " << exception.what() << endl; 
     return "err"; 
    } 
} 

bool writeGZipLog (char* data) { 
    using namespace boost::iostreams; 
    using namespace std; 
    try { 
     std::ofstream file(currentFile.c_str(), std::ios_base::app); 
     boost::iostreams::filtering_ostream out; 
     out.push(gzip_compressor()); 
     out.push(file); 
     stringstream strstream; 
     strstream << data; 
     boost::iostreams::copy(strstream, out); 
     return true; 
    } catch (const gzip_error& exception) { 
     cout << "Boost Description of Error: " << exception.what() << endl; 
     return false; 
    } 
} 

내가 말할 수있는 것은 내가 필요하지 않았다 약간의 오차가했고, 난 그냥보고에 의해 발견이다 :

은 (GZIP와의 bzip2와) 아무 문제없이 나를 위해 작동 코드입니다 나중에 몇 시간 후에 다시 코드를 작성하십시오. 예를 들어 boost::iostreams::copy(std::stringstream , char*);은 1 + 1이 3 일 경우에도 실패합니다.

이 코드 조각이 도움이되기를 바랍니다.

폴 :

관련 문제