streambuf

Discussion in 'C++' started by schultz, Feb 24, 2004.

  1. schultz

    schultz Guest

    "File streams are associated to a buffer of type streambuf of certain size."

    In what cases a file stream could have no associated buffer?
    How to determine and resize the size of a buffer?
    How to use manipulators, flush and endl to do synchronization?

    Thanks!
    schultz, Feb 24, 2004
    #1
    1. Advertising

  2. schultz

    Rolf Magnus Guest

    schultz wrote:

    > "File streams are associated to a buffer of type streambuf of certain
    > size."
    >
    > In what cases a file stream could have no associated buffer?


    According to the sentence you quote above, in none.

    > How to determine and resize the size of a buffer?


    I don't think you can determine the size portably, but you can resize
    the file by just writing beyond its end.

    > How to use manipulators, flush and endl to do synchronization?


    Synchronitation with what?
    Rolf Magnus, Feb 24, 2004
    #2
    1. Advertising

  3. schultz wrote:
    > In what cases a file stream could have no associated buffer?


    It is entirely the freedome of the person implementing the standard
    C++ library. However, I'm not aware of any implementation which does
    not have a buffer and I would expect any quality implementation to
    have a buffer suitable for the underlying file system by default. Of
    course, an implementation might choose to avoid a buffer when writing
    to some form of special files, eg. named pipes, but this is entirely
    out of the scope of the C++ standard.

    > How to determine and resize the size of a buffer?


    There is no way to determine the size of a file buf's buffer other
    than deriving from 'std::basic_filebuf' and inspecting the values
    of the buffer pointers ('pbase()' and 'epptr()' for the put area and
    'eback()' and 'egptr()' for the get area). However, you should probably
    avoid fiddling explicitly with these pointers.

    There is no portable way to guarantee a certain buffer size with the
    exception of turning of buffering by calling 'pubsetbuf(0, 0)'. You
    can feed a buffer and its size to the same function but the effect of
    this call is open to the implementation: it can simply ignore the
    request (unless both arguments are 0) which is exactly what I have done
    in my implementation (because it is hard enough to getting file buffers
    right when you can make at least a few basic assumptions...).

    > How to use manipulators, flush and endl to do synchronization?


    When you are done writing and you want the bytes written to be sent to
    the operating system, you should call 'pubsync()' on the underlying
    file buffer in some form: both 'std::flush' and 'std::endl' will have
    this effect. There are, however, also other means:

    - you can call 'std::basic_ostream<...>::flush()'
    - you can call 'std::basic_ios<...>::rdbuf()->pubsync()'
    - you can set the 'std::ios_base::unitbuf' flag in the formatting flags
    - you can read from a 'std::basic_ios<...>::tie()'d input stream

    Note, that all of these will have the effect of flushing the put buffer's
    content to the operating systems write function. There are a few things
    to note:

    - The buffer's content is converted according to the rules of the
    corresponding 'std::codecvt<...>' facet.
    - Only the output buffer is synchronized; there is no way to explicitly
    synchronize the input buffer eg. to reflect changes made by another
    process.
    - The buffer is only written to the operating system and it is at the
    discretion of the operating system when the data is sent to the
    underlying representation, eg. when it is written to the disc. There
    is no portable way to force immediate writing at the operating system
    level.

    --
    <mailto:> <http://www.dietmar-kuehl.de/>
    Phaidros eaSE - Easy Software Engineering: <http://www.phaidros.com/>
    Dietmar Kuehl, Feb 24, 2004
    #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. Davis King
    Replies:
    0
    Views:
    499
    Davis King
    Jul 23, 2003
  2. Matt Chaplain

    Problem with streambuf

    Matt Chaplain, Jul 30, 2003, in forum: C++
    Replies:
    4
    Views:
    443
    Matt Chaplain
    Jul 31, 2003
  3. =?ISO-8859-1?Q?Viktor_Lundstr=F6m?=

    Inheriting streambuf

    =?ISO-8859-1?Q?Viktor_Lundstr=F6m?=, Sep 24, 2003, in forum: C++
    Replies:
    3
    Views:
    3,672
    Dietmar Kuehl
    Sep 25, 2003
  4. Peter Jansson
    Replies:
    1
    Views:
    497
    David Rubin
    Nov 8, 2004
  5. Christopher Pisz
    Replies:
    2
    Views:
    587
    James Kanze
    Dec 12, 2007
Loading...

Share This Page