C++ 如何使用Boost IOStreams';Gzip文件的接口?
我成功地集成了boostiostreamapi来读取压缩文件。我遵循boost页面中的文档,到目前为止有以下代码:C++ 如何使用Boost IOStreams';Gzip文件的接口?,c++,boost,file-io,gzip,iostream,C++,Boost,File Io,Gzip,Iostream,我成功地集成了boostiostreamapi来读取压缩文件。我遵循boost页面中的文档,到目前为止有以下代码: std::stringstream outStr; ifstream file("file.gz", ios_base::in | ios_base::binary); try { boost::iostreams::filtering_istreambuf in; in.push(boost::iostreams::gzip_decompresso
std::stringstream outStr;
ifstream file("file.gz", ios_base::in | ios_base::binary);
try {
boost::iostreams::filtering_istreambuf in;
in.push(boost::iostreams::gzip_decompressor());
in.push(file);
boost::iostreams::copy(in, outStr);
}
catch(const boost::iostreams::gzip_error& exception) {
int error = exception.error();
if (error == boost::iostreams::gzip::zlib_error) {
//check for all error code
}
}
代码运行良好(因此请忽略上面的任何打字错误和错误:)
copy()
整个文件复制到字符串缓冲区outtr
。根据
函数模板副本从给定的源模型读取数据,并将其写入给定的接收器模型,直到流结束
2) 从filtering\u istream buf
切换到filtering\u istream
和std::getline()将起作用:
#include <iostream>
#include <fstream>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/filter/gzip.hpp>
int main()
{
std::ifstream file("file.gz", std::ios_base::in | std::ios_base::binary);
try {
boost::iostreams::filtering_istream in;
in.push(boost::iostreams::gzip_decompressor());
in.push(file);
for(std::string str; std::getline(in, str); )
{
std::cout << "Processed line " << str << '\n';
}
}
catch(const boost::iostreams::gzip_error& e) {
std::cout << e.what() << '\n';
}
}
#包括
#包括
#包括
#包括
int main()
{
std::ifstream文件(“file.gz”,std::ios_base::in | std::ios_base::binary);
试一试{
boost::iostreams::filtering\u istream in;
in.push(boost::iostreams::gzip_decompressor());
in.push(文件);
for(std::string str;std::getline(in,str);)
{
std::cout谢谢。让我试试这个。我希望将存储为类成员。并且在类中有一个名为getline的成员函数。getline应该能够从当前文件指针位置返回行。我尝试在循环中打印流位置,但它不起作用。file.tellg()
如果我使用std::ifstream
返回一个常量,如果我使用boost::iostream::file\u source
返回0。如果我在流中调用它,它将返回-1。我如何获得文件中的当前位置并能够移动到该位置?我必须首先将所有流转储到另一个流中吗?