2011-01-17 5 views
0

ファイルを解凍してその内容をストリングストリームに書きたいとします。圧縮されていない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

Icファイルを聞いた:それは何かが含まれていますが、私はgunzipを使用してungzipできません。だから私は圧縮が適切に機能しているかどうか、そしてこれがBoostによって投げられたgzip errorと関係があるかどうかはわかりません。

エラーが発生している箇所をヒットできますか?

ありがとうございました!

ポール

答えて

2

多くの研究の後、私は最終的に正しく(デ)圧縮を処理する仕方を発見したしようとしています。

これは、(gzipとbzip2で)何の問題もなく私のために動作するコードです:

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; 
    } 
} 

私が言えることは、私は必要はありませんでしたいくつかのエラーをしたと私はちょうどを見て、見つけたことです数時間後にもう一度コードを入力してください。例えば、1 + 1が3の場合、boost::iostreams::copy(std::stringstream , char*);は失敗します。

私はこのコードが誰かを助けてくれることを願っています。

ポール:

関連する問題