cpp*_*der 16 c++ file-io boost gzip iostream
我设法集成了boost Iostream API来读取压缩文件.我按照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_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
}
}
Run Code Online (Sandbox Code Playgroud)
代码工作正常(所以请忽略任何拼写错误.以及上面的错误:)).
Cub*_*bbi 22
1)是的,上面的代码将copy()整个文件放入字符串缓冲区outStr.根据副本的描述
函数模板副本从给定的Source模型中读取数据,并将其写入给定的Sink模型,直到到达流的末尾.
2)切换filtering_istreambuf到filtering_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';
}
}
Run Code Online (Sandbox Code Playgroud)
(你可以std::cout << file.tellg() << '\n';在那个循环内部,如果你想要证明.它会增加相当大的块,但它不会等于从开始的文件的长度)
| 归档时间: |
|
| 查看次数: |
18427 次 |
| 最近记录: |