473,397 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Stream buffers and rdbuf() question

Suppose I have something like this:

class foo {
std::istream in;
public:
foo(std::istream& in_) : in(in_.rdbuf());
void doStuffWith(std::ostream& out);

void bar();
};

where doStuffWith reads stuff from in and writes stuff to out.

Now, suppose bar() was like this:

void foo::bar() {
std::ostringstream out;
doStuffWith(out);
in.rdbuf(out.rdbuf());

// read stuff from in
}

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. Why is
this going on, and how can I fix it so it does what I expect it to do?

--
I am only a mirage.
Mar 9 '06 #1
2 2715
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.
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.


Can you post complete code which exhibits this problem? It sounds
extremely unlikely and I don't see any reason why this should
happing.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
Mar 9 '06 #2
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.
Mar 10 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Gabriel Schreiber | last post by:
Hy I need some help on this problem: I want to send some output to a number of streams. So I need an object, that behaves (or is) like an std::ostream. (additional wish: the object is an...
7
by: Stewart Gordon | last post by:
Using g++ 3.1, Mac OS X. I'm doing something trying to involve a null output stream, i.e. a stream that just discards what goes into it. The idea is that it gives a simple way of producing...
2
by: Noah Roberts | last post by:
It was my understanding that doing something like 'cin.rdbuf()->pubsetbuf(NULL, 0)' would cause any buffering troubles to stop. I am not sure where the problem resides, but I am experiencing...
24
by: Hendrik Schober | last post by:
Hi, I have a 'std::istream' and need to read its whole contents into a string. How can I do this? TIA; Schobi
2
by: Zaharije Pasalic | last post by:
What is purpose of rdbuf methode of stream class? Is rdbuf create new buffer or just return some kind of pointer to existing? Thanks, Zaharije Pasalic
5
by: Sandy | last post by:
Hi, In one of my interview i was asked the difference between Stream and Buffer. can anybody explain the difference. Thanks
18
by: JG | last post by:
Does anyone know a standard (or supported on Linux, Mac, Win32) way to clear a read stream buffer (standard ANSI C file stream)? I would even settle for a platform specific way of doing it. ...
4
by: rakesh.usenet | last post by:
For a particular application of mine - I need a simulation of byte array output stream. * write data onto a stream * getback the contiguous content as an array later for network transport. ...
7
by: mathieu | last post by:
Hi there, I am trying to rewrite this very slow function (due to AFAIK the extra copy): void DoAction(std::istream &is, std::ostream &os) { uint16_t c; while( is.read((char*)&c,2) )
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.