c++ - boost::iostreams, gzip files and tellg -
i have been using boost::iostreams reading uncompressed text file. in application, need multiple file handles (stored in map) efficiently buffer data different parameters stored in file. also, if read line , parameter @ time later interested in, restore stream position (recovered via tellg()) before called getline() can still buffer value @ future time. however, wish read gzip compressed files, otherwise perform identical operations before. have run across following issues (discovered before, solution not seem work triplet of requirements). short test main() reproduces these issues follows:
#include <iostream> #include <iomanip> #include <fstream> #include <string> #include <boost/shared_ptr.hpp>// shared_ptr #include <boost/iostreams/filtering_stream.hpp>// filtering_[io]stream #include <boost/iostreams/filter/gzip.hpp>// gzip //----------------------------------------------------------------------------- int main( int argc, char** argv ){ std::cout << std::scientific << std::setprecision(15) << std::endl; std::string filename("test.txt.gz"); bool gzipped(true); std::string line; // test 1 boost::shared_ptr<std::ifstream> filestream1; boost::shared_ptr<boost::iostreams::filtering_istream> filefilter1; filefilter1.reset( new boost::iostreams::filtering_istream ); filestream1.reset( new std::ifstream( filename.c_str() ) ); if(gzipped) filefilter1->push( boost::iostreams::gzip_decompressor() ); filefilter1->push( *filestream1 ); while( std::getline( *filefilter1, line ) ){ //std::streampos strpos( filefilter1->tellg() );// uncomment line run-time errors std::cout << line << std::endl; } std::cout << std::endl; // test 2 boost::shared_ptr<std::ifstream> filestream2; boost::shared_ptr<boost::iostreams::filtering_stream<boost::iostreams::input_seekable> > filefilter2; filefilter2.reset( new boost::iostreams::filtering_stream<boost::iostreams::input_seekable>() ); filestream2.reset( new std::ifstream( filename.c_str() ) ); //filefilter2->push( boost::iostreams::gzip_decompressor() );// uncomment line compile-time errors filefilter2->push( *filestream2 ); while( std::getline( *filefilter2, line ) ){ std::streampos strpos( filefilter2->tellg() ); std::cout << line << std::endl; } std::cout << std::endl; return 0; }
the input file in case can contain whatever like. make sure has >1 lines of text see tellg() issue.
in test1, can imagine errors caused tellg()'s sentry creation , failbit modification (http://www.cplusplus.com/reference/istream/istream/sentry/)?!? post (https://svn.boost.org/trac/boost/ticket/2449) suggests that:
boost::iostreams::filtering_stream<boost::iostreams::input_seekable>
will overcome tellg() issue. however, test2 shows unable push decompressor object onto type of filter. have not found workaround this.
this first time posting here, apologies if question has formatting issues!
many can provide, , time may spend musing!
Comments
Post a Comment