Home

streambufs

Streambufs, short for stream buffers, are the foundational components of the C++ iostreams library. A stream buffer (std::streambuf) provides the actual character storage and the interface through which input and output streams access that storage. It sits between higher-level stream objects (std::istream, std::ostream, and their typedefs) and the underlying data sources or destinations, such as files, memory blocks, or strings, enabling buffering, reading, and writing to be performed efficiently and abstractly.

A streambuf maintains separate regions for input (get area) and output (put area). Typical protected members

Concrete stream buffers include std::filebuf (and std::basic_filebuf) for file I/O, and std::basic_stringbuf (often accessed via std::stringstream

In summary, streambufs encapsulate the buffering and character sequencing logic that underpins the C++ iostreams framework,

include
pointers
for
the
get
area:
gbase,
gptr,
and
egptr,
and
pointers
for
the
put
area:
pbase,
pptr,
and
epptr.
The
get
pointers
define
the
available
input
characters,
while
the
put
pointers
define
the
space
allocated
for
buffered
output.
The
class
provides
a
small
set
of
virtual
functions
that
derived
classes
override
to
implement
specific
behavior,
notably
underflow/uflow
for
input
and
overflow
for
output,
which
handle
refilling
and
flushing
the
buffer.
Additional
virtuals
include
sync,
seekoff,
and
seekpos
to
coordinate
with
the
underlying
device,
plus
xsgetn
and
xsputn
for
bulk
operations.
Public
helpers
such
as
pubsetbuf
and
setg/setp
allow
configuring
the
buffers
and
initial
pointers.
or
std::stringbuf)
for
string-based
I/O.
Users
typically
interact
with
streams
rather
than
buffers
directly,
but
custom
stream
buffers
can
be
derived
to
adapt
streams
to
devices,
network
protocols,
or
special
memory
layouts
by
overriding
the
relevant
virtual
functions
and
providing
or
managing
a
buffer.
enabling
flexible,
efficient,
and
extensible
input
and
output.