Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/138.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如果我试图将未压缩的过滤流复制到stringstream,则会崩溃_C++_Segmentation Fault_Gzip_Boost Iostreams - Fatal编程技术网

C++ 如果我试图将未压缩的过滤流复制到stringstream,则会崩溃

C++ 如果我试图将未压缩的过滤流复制到stringstream,则会崩溃,c++,segmentation-fault,gzip,boost-iostreams,C++,Segmentation Fault,Gzip,Boost Iostreams,我想解压一个文件并将其内容写入stringstream 这是我尝试的代码: 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.p

我想解压一个文件并将其内容写入stringstream

这是我尝试的代码:

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;
    }
 }
/build
是自动编译和启动应用程序
/test
的脚本

我检查了文件:它包含一些内容,但我无法使用
gunzip
将其解压缩。因此,我不确定压缩是否正常工作,以及这是否与Boost抛出的
gzip错误有关

你能告诉我错误在哪里吗

谢谢你的帮助


Paul

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

这是适用于我的代码,没有任何问题(使用gzip和bzip2):

string readgzip日志(){
使用名称空间boost::iostreams;
使用名称空间std;
试一试{
ifstream文件(currentFile.c_str(),ios_base::in | ios_base::binary);
boost::iostreams::filtering\u istream in;
in.push(gzip_decompressor());
in.push(文件);
stringstream和strstream;
boost::iostreams::copy(in,strstream);
返回strstrstream.str();
}捕获(const gzip_错误和异常){
库特
gzip error
./build: line 3: 22174 Segmentation fault      ./test
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;
    }
}