Boost logo

Boost Users :

Subject: Re: [Boost-users] [iostreams] How can I use the Stream Insertion operator (<<) with a filtering_streambuf?
From: Ken Appleby (ken.appleby_at_[hidden])
Date: 2013-07-26 02:09:30


On 26/07/2013 01:37, Stephen Greenfield wrote:
> Partially asked in another question, but so far unanswered: Once I create
> a
> filtering_streambuf that is connected to the zlib_compressor() and the
> output file,
> how do I then construct an object that can accept data from the Stream
> Insertion Operator?
>
>
> void CompressUsingFilteringStreambuf()
> {
> std::ofstream myFile("hello.z", std::ios_base::out |
> std::ios_base::binary);
>
> boost::iostreams::filtering_streambuf<boost::iostreams::output> out;
> out.push(boost::iostreams::zlib_compressor());
> out.push(myFile);
>
> // MY QUESTION: How can I use the "stream insertion" operator
> (<<)
> // to go through the filtering_streambuf
> // and place compressed text into myFile?
>
> // HOW DO I CREATE someStream?
> someStream << "text I want to be compressed and end up in the
> file...";
>
> myFile.close();
> }
>
> I have scoured the documentation and the web looking for the answer -- and
> come up empty.
>
> Stephen
>

Try:

  std::ofstream myFile("hello.z", std::ios_base::out |
std::ios_base::binary);
  boost::iostreams::filtering_streambuf<boost::iostreams::output> out;
  out.push(boost::iostreams::zlib_compressor());
  out.push(myFile);

  std::ostream os(&out);
  os << "text I want to be compressed and end up in the file..."

On input it would be something like:

  std::ifstream myFile( "hello.z", std::ios::in | std::ios::binary );

  boost::iostreams::filtering_streambuf< boost::iostreams::input > in;
  in.push( boost::iostreams::gzip_decompressor() );
  in.push( ifs );
  std::istream is( &in );
  is >> text;

Ken Appleby

Soft Optics Ltd


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net