In article <47************@individual.net>, Dietmar Kuehl
<di***********@yahoo.com> wrote:
kelvSYC wrote:
std::ostringstream out;
doStuffWith(out);
in.rdbuf(out.rdbuf());
At best, the stream buffer created for 'out' is not constructed
to perform input operations. The standard definitely mentions that
depending on the "mode" flags passed the stream is for reading or
writing. It does not, however, spell out what happens when the
stream is used in violation to these flags.
True. As I found out with further experimentation, you'd need to copy
the buffer with something like:
std::stringbuf buf(out.str());
in.rdbuf(&buf);
after doStuffWith(out), or instead have
std::stringbuf outbuf;
std::ostream out(&outbuf);
doStuffWith(out);
in.rdbuf(out.rdbuf());
.... guess I was unaware of the completely obvious observation that
streambufs that are created with ostreams are built with only out-mode
enabled (and similarly for istreams).
It still smells of real redundancy with istreams, ostreams, and
iostreams (not to mention the counterintuitive fact that an iostream is
descended from istream and ostream but an fstream is not a descendant
of ifstream and ofstream), as you're now dealing at the streambuf
level. (This is one of those times I wish C++ had say Java's streaming
classes...)
and a foo object was instantiated with an std::ifstream. At the end
of bar(), we would expect that you would effectively read the
characters that were put into out from doStuffWith(). Yet, I keep
reading from the (beginning of the) original filebuf instead.
The only plausible explanation that I can come up with this behavior is
that setting the streambuf resets the streambuf's buffer pointers, and
that some streambufs keep separate input and output buffers. That
still doesn't explain why in.rdbuf(out.rdbuf()) had no effect, even if
the streambuf replacement was successful (and a filebuf was replaced by
a stringbuf).
--
I am only a mirage.