如果我尝试将未压缩的filtering_istream复制到stringstream,则会发生崩溃

发布于 2024-10-12 08:18:44 字数 1407 浏览 4 评论 0原文

我想解压缩文件并将其内容写入字符串流。

这是我尝试过的代码:

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 解压它。所以我不确定压缩是否正常工作,以及这是否与 Boost 抛出的 gzip 错误 有关。

您能给我指出错误所在吗?

感谢您的帮助!

保罗

I want to uncompress a file and write its content into a stringstream.

This is the code I tried:

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

It compiles without any warnings (and of course errors) but the function readGZipLog() crashes while running:

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

./build is the script that compiles and starts the application ./test automatically

I checked the file: It contains something, but I can't ungzip it using gunzip. So I am not sure whether the compression worked properly and if this has something to do with the gzip error thrown by Boost.

Can you give me a hit where the error(s) is(/are)?

Thanks for your help!

Paul

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

椒妓 2024-10-19 08:18:44

经过大量研究和尝试,我终于找到了一种正确处理(解)压缩的方法。

这是对我来说没有任何问题的代码(使用 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* ); 甚至会失败。

我希望这段代码能够帮助别人,就像它帮助我一样。

保罗:)

after a lot of research and trying I finally found a way how to handle (de)compression correctly.

This is the code that works for me without any problems (with gzip and 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;
    }
}

What I can say is that I did some errors that were not necessary and I just found by looking at the code again many hours later. boost::iostreams::copy( std::stringstream , char* ); for example will even fail if 1 + 1 was 3.

I hope that this code piece will help somebody as it helped me.

Paul :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文