Boost logo

Boost Users :

Subject: Re: [Boost-users] [iostreams] How can I use the Stream Insertion operator (<<) with a filtering_streambuf?
From: Stephen Greenfield (StephenG_at_[hidden])
Date: 2013-07-26 22:26:51


SUCCESS!

A combination of hints from all who replied made the difference:

1) Frédéric pointed out that "nothing happen[s] until output_file [filtering_ostream] is destroyed. So enclose in { }". This was also key, because I was trying to do file.close() on my ofstream BEFORE my filtering_streambuf was destroyed. That explains why the file was empty!

Re-reading the documentation reveals:

"By default, if the Device at the end of the chain is popped
or if the filtering_stream is complete when it is destroyed,
all the filters and devices in the chain are closed using the
function close. This behavior can be modified using the member
function set_auto_close"

This states is there is no need to "pop" the compressor() or the ofstream off the filtering_stream's stack, nor to call close(). Just destruct the filtering_stream object, and everything gets written out and cleaned up.

2) Ken Appleby's source code was very complete, and allowed me to confirm the proper sequence of creating the ostream from the filtering_streambuf.

3) Holger Gerth questioned why I was using filtering_streambuf when I could've been using filtering_stream. Truth is, I wasn't sure, however, in my experiments I could neither construct the ostream (which I required to pass to other functions) from the filtering_stream, nor could I pass the filtering_stream in place of the ostream I required.

Even after reading several articles on filtering_streambuf vs filtering_stream, I'm still mystified how and why (FOR MY PURPOSE) I would use the filtering_stream over constructing an ostream from a filtering_streambuf.

Thanks for all the help -- I REALLY could not have done it without your collective wisdom.

Stephen





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