
On 26/07/2013 14:49, Larry Evans wrote:
On 07/26/13 06:39, Frédéric Bron wrote:
This is how I do it with file_sink:
boost::iostreams::filtering_ostream output_file; output_file.push(boost::iostreams::bzip2_compressor(boost::iostreams::bzip2_params(9))); output_file.push(boost::iostreams::file_sink("file.bz2", std::ios_base::binary)); output_file<<"blalba";
I think if you do just this, nothing happen until output_file is destroyed. So enclose in { }.
Frédéric
Thanks Frederic.
However, I think this is not obvious. I would have thought Ken's solution would have worked. I would have also thought mine would have worked until I saw Ken's which indicated std::ios_base::binary was needed when the std::ofstream was created.
Is there anywhere in the docs explaining why your method works and Ken's doesn't?
-regards, Larry
Larry, Passing the filtering_streambuf to a stream constructor should indeed work, as you expect. The test program below works fine for me - boost 1_49 on mingw 4.4. Ken Appleby Soft Optics Ltd ------------------- #include <iostream> #include <fstream> #include <boost/iostreams/filtering_streambuf.hpp> #include <boost/iostreams/copy.hpp> #include <boost/iostreams/filter/gzip.hpp> int main() { try { { std::ofstream myFile("hello.gz", std::ios_base::out | std::ios_base::binary); boost::iostreams::filtering_streambuf<boost::iostreams::output> out; out.push(boost::iostreams::gzip_compressor()); out.push( myFile ); std::ostream os(&out); os << "text I want to be compressed and end up in the file..."; } { std::ifstream myFile( "hello.gz", std::ios::in | std::ios::binary ); boost::iostreams::filtering_streambuf< boost::iostreams::input > in; in.push( boost::iostreams::gzip_decompressor() ); in.push( myFile ); 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; } }