Сбой при попытке скопировать несжатый filtering_istream в stringstream - PullRequest
0 голосов
/ 18 января 2011

Я хочу распаковать файл и записать его содержимое в поток строк.

Это код, который я пробовал:

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 автоматически

Я проверил файл: он содержит что-то, но я не могу разархивировать его, используя gunzip. Поэтому я не уверен, что сжатие работало правильно, и связано ли это с gzip error, генерируемым Boost.

Можете ли вы дать мне удар, где ошибка (и) (*)?

Спасибо за вашу помощь!

Пол

1 Ответ

2 голосов
/ 18 января 2011

после долгих исследований и попыток я наконец нашел способ, как правильно обрабатывать (де) сжатие.

Этот код работает для меня без проблем (с 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;
    }
}

Что я могу сказать, так это то, что я сделал несколько ошибок, которые не были необходимы, и я просто нашел, посмотрев на код снова много часов спустя.boost::iostreams::copy( std::stringstream , char* ); например, даже потерпит неудачу, если 1 + 1 было 3.

Я надеюсь, что этот фрагмент кода кому-нибудь поможет, так же, как и мне.

Пол :))

...