Home / Cplusplus / Streams In C++

Streams In C++

This section covers the basics of using streams, including explaining which streams are provided by the streams library, how the different streams classes are related, which member functions are available for use with streams, and how to create your own streams.
Streams provided by the library
The streams library provides four different kinds of streams:
where characters are read and written to areas in memory.
where characters are read and written to external files.
where characters are read and written to external files using the C standard I/O library.
where a bsambuf object is used for performing formatted file I/O.
stdiostream objects should be used in programs that use the C standard I/O package as well as C++, to avoid interference between the two forms of I/O; on some implementations, fstreams provide better performance than stdiostreams when interaction with C I/O is not an issue.
Other types of streams can be defined by derivation from the base classes iostream and streambuf.See Stream class hierarchy for more information on the relationships between these classes.
Every C++ program begins execution with four defined streams.
is a standard source of input. It reads input from the same place that stdin would have.
is a stream to which program output can be written. It writes output to the same place stdout would have.
is a stream to which program error output can be written. It writes output to the same place stderr would have.
is another error stream that can be more highly buffered than cerr. It also writes output to the same place stderr would have.
To use these streams, you must include the header file iostream.h.
Additional streams can be created by the program as necessary. For more information, see Creating streams.
Stream class hierarchy
All the different stream classes are derived from two common base classes: ios and streambuf.
class ios is a base class for the classes istream (an input stream), ostream (an output stream) andiostream (a bidirectional stream). You are more likely to use these classes as base classes than to use class ios directly.
The streambuf class is a class that implements buffering for streams and controls the flushing of a full output buffer or the refilling of an empty input buffer.
Four sets of stream classes are provided in the standard streams library: fstream, strstream, stdiostream, and bsamstream.
Corresponding to each of these stream classes is a buffer class: filebuf, strstreambuf, stdiobuf, and bsambuf, implementing a form of buffering appropriate to each stream.
Note that the stream classes are not derived from the buffering classes; rather, a stream object has an associated buffering object of the appropriate kind (for instance, an fstream object has an associated filebuf ), which can be accessed directly if necessary using the rdbuf() member function.
Relationship between Stream Classes shows the inheritance relationships between the various classes.
Relationship between Stream Classes
. .