//Purpose: // Explore differences in ways iostreams filtering is done // with different device types. //Acknowledgements: // Original code from post: /* From: Ken Appleby Newsgroups: gmane.comp.lib.boost.user Subject: Re: [iostreams] How can I use the Stream Insertion operator (<<) with a filtering_streambuf? Date: Fri, 26 Jul 2013 18:06:47 +0100 Lines: 94 */ //========================== #include #define USE_STRINGSTRM //use stringstream instead of fstream. #ifdef USE_STRINGSTRM #include #else #include #endif #include #include #include int main() { try { #ifdef USE_STRINGSTRM std::string strmValue; #else char const*filePath="hello.gz"; #endif { #ifdef USE_STRINGSTRM std::ostringstream myOstrm( strmValue, std::ios_base::out | std::ios_base::binary ); #else std::ofstream myOstrm( filePath , std::ios_base::out | std::ios_base::binary ); #endif boost::iostreams::filtering_streambuf out; //#define USE_FILT #ifdef USE_FILT out.push(boost::iostreams::gzip_compressor()); #endif out.push( myOstrm ); std::ostream os(&out); os << "text I want to be compressed and end up in the file..."; os.flush(); strmValue=myOstrm.str(); } #ifdef USE_STRINGSTRM std::cout<<"strmValue="< in; #ifdef USE_FILT in.push( boost::iostreams::gzip_decompressor() ); #endif in.push( myIstrm ); std::istream is( &in ); std::string text; while ( is.good() ) { char ch = is.get(); if ( is.good() ) text.push_back( ch ); } std::cout << "text from file is \"" << text << "\"" << std::endl << std::flush; } } catch ( std::exception& e ) { std::cerr << e.what() << std::endl; } }