Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Share Stream Buffer?

Reply
Thread Tools

Share Stream Buffer?

 
 
Immortal Nephi
Guest
Posts: n/a
 
      04-06-2010
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.
 
Reply With Quote
 
 
 
 
Immortal Nephi
Guest
Posts: n/a
 
      04-07-2010
On Apr 6, 6:32*pm, Sam <s...@email-scan.com> wrote:
> Immortal Nephi writes:
> > * *cout object has stream buffer. *It is derived from ostream object.
> > How many characters do stream buffer hold?

>
> Implementation defined.
>
> > * * * * * * * * * * * * * * * * * * * * * * *Or…do ostream object
> > resize to increase stream buffer’s memory size if characters are full?

>
> 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.
>
> > * *Is there a way to reread stream buffer and output to the screen
> > second time?

>
> 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.
>
>
>
> > 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

>
> 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?

> > 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.

>
> 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: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?
 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      04-09-2010
On Apr 7, 12:13 am, Immortal Nephi <Immortal_Ne...@hotmail.com> wrote:
> 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.

--
James Kanze

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Share-Point-2010 ,Share-Point -2010 Training , Share-point-2010Hyderabad , Share-point-2010 Institute Saraswati lakki ASP .Net 0 01-06-2012 06:39 AM
what is the different between byte stream and character stream? dolphin Java 6 03-18-2007 01:58 PM
get stream mode flags from an opened stream Alexander Korsunsky C++ 1 02-17-2007 10:38 AM
How to GET multi-word input from a *file* stream as opposed to a *console* stream? sherifffruitfly@gmail.com C++ 9 04-27-2006 04:14 PM
Doing readline in a thread from a popen4('rsync ...') stream blocks when the stream ends. Rasmusson, Lars Python 1 04-30-2004 08:10 AM



Advertisments