Share Stream Buffer?

Discussion in 'C++' started by Immortal Nephi, Apr 7, 2010.

  1. 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.
     
    Immortal Nephi, Apr 7, 2010
    #1
    1. Advertising

  2. On Apr 6, 6:32 pm, Sam <> 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::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?
     
    Immortal Nephi, Apr 7, 2010
    #2
    1. Advertising

  3. Immortal Nephi

    James Kanze Guest

    On Apr 7, 12:13 am, Immortal Nephi <> 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
     
    James Kanze, Apr 9, 2010
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Raja
    Replies:
    12
    Views:
    24,702
    John Harrison
    Jun 21, 2004
  2. Saraswati lakki
    Replies:
    0
    Views:
    1,410
    Saraswati lakki
    Jan 6, 2012
  3. Jack
    Replies:
    2
    Views:
    244
  4. Eric Wong
    Replies:
    0
    Views:
    223
    Eric Wong
    Feb 13, 2010
  5. Roedy Green

    byte stream vs char stream buffer

    Roedy Green, May 7, 2014, in forum: Java
    Replies:
    20
    Views:
    266
    Silvio
    May 18, 2014
Loading...

Share This Page