如果我尝试将未压缩的filtering_istream复制到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;
}
}
它编译时没有任何警告(当然还有错误),但函数 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
经过大量研究和尝试,我终于找到了一种正确处理(解)压缩的方法。
这是对我来说没有任何问题的代码(使用 gzip 和 bzip2):
我可以说的是我犯了一些不必要的错误,我只是在几个小时后再次查看代码才发现。例如,如果 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):
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 :)