How can I read line-by-line using Boost IOStreams' interface for Gzip files?

淺唱寂寞╮ 提交于 2019-11-30 00:25:22

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)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!