我怎么能读行由行使用Boost输入输出流“界面的gzip文件? [英] How can I read line-by-line using Boost IOStreams' interface for Gzip files?

查看:356
本文介绍了我怎么能读行由行使用Boost输入输出流“界面的gzip文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设法升压的iostream集成的API读取压缩文件。我跟着提升页面的文档,并具有以下code迄今为止在:

 的std :: stringstream的outStr;
ifstream的文件(file.gz的ios_bas​​e ::在|的ios_bas​​e ::二进制);
尝试{
    提高::输入输出流:: filtering_istreambuf的;
    in.push(升压::输入输出流:: gzip_decom pressor());
    in.push(文件);
    提高::输入输出流::复制(在,outStr);
}
抓(常量的boost ::输入输出流:: gzip_error&安培;除外){
    INT错误= exception.error();
    如果(错误==的boost ::输入输出流:: gzip的:: zlib_error){
       //检查所有的错误code
    }
}

在code正常工作(所以请忽略任何错别字。与上述错误:))。


  1. 看起来像上面的code将读取完整的文件,并将其存储在存储器中,而创建filtering_istreambuf。这是真的,从我的调查,它看起来这么对我?如果该文件被读入内存,这code能对大文件的问题(这是我处理)。

  2. 我现在的code读取使用gzgets API从zlib的一行行的gzip压缩。有没有办法通过使用boost的API线阅尽行?


解决方案

1)是,上述code将复制()整个文件写入字符串缓冲 outStr 。根据副本的描述


  

函数模板拷贝从源的一个给定的模型中读取数据并将其写入到接收器的一个给定的模型,直到达到流的末尾。


2) filtering_istreambuf 开关置于 filtering_istream 和std ::函数getline()将工作:

 的#include<&iostream的GT;
#包括LT&;&的fstream GT;
#包括LT&;升压/输入输出流/ filtering_stream.hpp>
#包括LT&;升压/输入输出流/过滤/ gzip.hpp>
诠释的main()
{
    性病:: ifstream的文件(file.gz的std :: ::的ios_bas​​e在|的std :: ::的ios_bas​​e二进制);
    尝试{
        提高::输入输出流:: filtering_istream的;
        in.push(升压::输入输出流:: gzip_decom pressor());
        in.push(文件);
        对于(性病::字符串str;的std ::函数getline(中,STR);)
        {
            性病::法院LT&;< 加工线<< STR<<的'\\ n';
        }
    }
    抓(常量的boost ::输入输出流:: gzip_error急症){
         性病::法院LT&;< e.what()&所述;&下;的'\\ n';
    }
}

(你可以的std ::法院<< file.tellg()<<'\\ n'; 如果你想证明,循环里面。将在相当大的块增加,但它不会是相等的文件从一开始的长度)

I managed to integrate the boost Iostream APIs for reading zipped files. I followed the documentation in boost page and have the following code so-far:

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

The code works fine (so please ignore any typos. and errors above :)).

  1. Looks like the above code will read the complete the file and store it in the memory while creating the filtering_istreambuf. Is that true, from my investigation it looks so to me? If the file is read into memory, this code can be an issue for large files (which is what I'm dealing with).
  2. My current code reads the gzipped using gzgets API from zlib line by line. Is there a way to do line by line reading using boost APIs?

解决方案

1) Yes, the above code will copy() the entire file into the string buffer outStr. According to the description of copy

The function template copy reads data from a given model of Source and writes it to a given model of Sink until the end of stream is reached.

2) switch from filtering_istreambuf to filtering_istream and std::getline() will work:

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

(you can std::cout << file.tellg() << '\n'; inside that loop if you want proof. It will increase in sizeable chunks, but it won't be equal the length of the file from the start)

这篇关于我怎么能读行由行使用Boost输入输出流“界面的gzip文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆