Share Stream Buffer?

I

Immortal Nephi

cout object has stream buffer. It is derived from ostream object.
How many characters do stream buffer hold? Or…do ostream object
resize to increase stream buffer’s memory size if characters are full?
Is there a way to reread stream buffer and output to the screen
second time?

For example:

cout << “Hello World!”;
cout // ?? reread stream buffer

“Hello World!” is inserted into stream buffer. I did not add endl
manipulator because I did not want to flush stream buffer. Can I copy
cout’s stream buffer to another stream buffer before I flush cout’s
stream buffer?
Maybe I want cout object and fstream object to share one stream
buffer. Stream buffer is sent to the console screen while it is
written to the disk at the same time.
If you say yes there is a way, please post your example source code.
I am not too sure if streambuf object is the answer.
 
I

Immortal Nephi

Implementation defined.


Implementation defined. Most implementations use a fixed default buffer
size. Once the buffer is full, the buffer's contents are flushed to the
underlying system file.


You could use rdbuf() to obtain a pointer to the stream buffer, and use its
method to get the current head or tail pointer.

However, that would be of very little use. Since the implementation may use
whatever buffer flushing strategy it feels like using, you have no
guarantees whatsoever what you'll get. You may find yourself with a buffer
that contains everything that was written to the stream, since its
instantiation. Or, you may find yourself with an empty buffer, because its
contents have just been flushed, completely.






This is true, however std::endl merely guarantees that the stream gets
flushed, if the stream is set to flush at endl. Your implementation is free
to flush the stream buffer at other times, so you may find that, for
whatever reason, your stream buffer chose to flush itself right after the
exclamation mark got inserted.

This may not be true any more, but at least in the past glibc set cerr by
default to be completely unbuffered. Every operator<<(), essentially,
did a flush().

Are you saying? If endl manipulator is omitted from cerr, then
operator<<() automatically flush stream buffer because cerr is
unbuffered. Correct?
The only portable way to do this is to write your content twice, yourself,
once to cout, and a second time to another stream.

Or, you can always write your content to a std::eek:stringstream, then retrieve
everything as a single blob using str(), then write the resulting string to
cout and the other stream.

If I want to create my own ostream object, I should use streambuf
object. Right? Is streambuf the same as filbuf, but the difference
is that streambuf sends to the console screen and filbuf sends to the
file.

What is the difference between sstream object and strstream object?

If I use the prefix basic_ object, can I choose any type such as int
and float instead of char and wchar_t?
 
J

James Kanze

cout object has stream buffer. It is derived from ostream object.
How many characters do stream buffer hold?

Whatever the implementation happens to decide. Normally, if
cout is connected to an interactive device, I'd expect it to be
unit buffered, which means that it will be flushed at the end of
every << operator.
Or do ostream object
resize to increase stream buffer’s memory size if characters are full?

An ostream object doesn't do anything with the buffer (except to
call flush sometimes); it doesn't know anything about the
buffering strategies used.
Is there a way to reread stream buffer and output to the screen
second time?

It depends on the stream buffer, but in most cases, no.
For example:
cout << "Hello World!";
cout // ?? reread stream buffer
"Hello World!" is inserted into stream buffer. I did not add endl
manipulator because I did not want to flush stream buffer.

You can force flushes when you need them, but you cannot prevent
them.
Can I copy
cout’s stream buffer to another stream buffer before I flush cout’s
stream buffer?
No.

Maybe I want cout object and fstream object to share one stream
buffer. Stream buffer is sent to the console screen while it is
written to the disk at the same time.

Those are two different things. Several ostream objects can
share the same streambuf; I actually do this fairly often. But
that means that text output through the different ostream
objects goes to the same destination. If you want to send the
same text to two different destinations, the classical solution,
and probably the simplest, is a filtering streambuf.
If you say yes there is a way, please post your example source
code. I am not too sure if streambuf object is the answer.

If you're concerned about where data is sinked, then streambuf
is the answer. A complete explination is a bit long for a
simple posting, but if you Google for it, you'll find all the
information you need. Boost also has a very good
implementation.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,149
Members
46,695
Latest member
StanleyDri

Latest Threads

Top