Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

iosintro Introduction to buffering, formatting, and input/output




This section describes the mechanisms that may be used to implement input and output in C++. The standard I/O library is written in C++ and shows the power of this programming language.

The C++ iostream package consists primarily of a collection of classes declared in the following header files:
<iostream.h>, <fstream.h>, <strstream.h>, <stdiostream.h>, <iomanip.h>. Although originally intended only to support input/output, the package now supports related activities such as byte array processing.


#include <iostream.h>

typedef long streampos, streamoff;

class streambuf;
class ios;
class istream : virtual public ios;
class ostream : virtual public ios;
class iostream : public istream, public ostream;
class istream_withassign : public istream;
class ostream_withassign : public ostream;
class iostream_withassign : public iostream;

extern istream_withassign cin;
extern ostream_withassign cout;
extern ostream_withassign cerr;
extern ostream_withassign clog;

#include <fstream.h>
class filebuf : public streambuf;
class fstreambase : virtual public ios;
class fstream : public fstreambase, public iostream;
class ifstream : public fstreambase, public istream;
class ofstream : public fstreambase, public ostream;

#include <strstream.h>
class strstreambuf : public streambuf;
class strstreambase : virtual public ios;
class istrstream : public strstreambase, public istream;
class ostrstream : public strstreambase, public ostream;
class strstream : public strstreambase, public iostream;

#include <stdiostream.h>
class stdiobuf : public streambuf;
class stdiostream : public ios;

#include <iomanip.h>



In the iostream package, there are some functions which return characters, but whichuse int as a return type. int is used so that all possible characters in the machine character set can be returned, as well as the value EOF as an error indication. A character is usually stored in a location of type char or unsigned char.

The iostream package consists of several core classes, which provide the basic functionality for I/O conversion and buffering, and several specialized classes derivedfrom the core classes. Both groups of classes are listed below.

The header file <iomanip.h> supplies macro definitions which programmers can useto define new parameterized manipulators (see manip).

Core classes

The core of the iostream package comprises the following classes:

streambuf

This is the base class for buffers. It supports insertion (also known as storing or putting) and extraction (also known as fetching or getting) of characters. Most members are inlined for efficiency. The public interface of class streambuf is described in sbufpub and the protected interface (for derived classes) is described insbufprot.

ios

This is the base class for stream input/output in C++. This class contains state variables that are common to the various stream classes, for example, error states and formatting states. See ios.

istream

This class supports formatted and unformatted conversion from sequences of characters fetched from streambufs. See istream.

ostream

This class supports formatted and unformatted conversion to sequences of characters stored into streambufs. See ostream.

iostream

This class combines istream and ostream. It is intended for situations in which bidirectional operations (inserting into and extracting from a single sequence of characters) are desired. See ios.

istream_withassign
ostream_withassign
iostream_withassign

These classes add assignment operators and a constructor with no operands to thecorresponding class without assignment. The predefined streams (see below) cin, cout, cerr, and clog, are objects of these classes. See istream, ostream, and ios.

Predefined streams

The following streams are predefined:

cin

The standard input (file descriptor 0), similar to stdin in the C language.

cout

The standard output (file descriptor 1), similar to stdout in the C language.

cerr

Standard error (file descriptor 2). Output through this stream is unit-buffered, which means that characters are passed to the C runtime system after each insert operation. (See ostream::osfx() in ostream and ios::unitbuf in ios.) It is like stderr in the C language.

clog

This stream is also directed to file descriptor 2, but unlike cerr its output is buffered.

cin, cerr and clog are tied to cout so that any use of these causes cout to be flushed.

In addition to the core classes enumerated above, the iostream package contains additional classes derived from them and declared in other headers. Programmers canuse these, or they may choose to define their own classes derived from the core iostream classes.

Classes derived from streambuf

Classes derived from streambuf define the details of how characters are produced orconsumed. Derivation of a class from streambuf (the protected interface) is discussed in sbufprot. The available buffer classes are:

filebuf

This buffer class supports I/O through file descriptors. Member functions support opening, closing, and seeking. Common uses do not require the program to manipulate file descriptors. See filebuf.

stdiobuf

This buffer class supports I/O through stdio FILE structs. It is intended for use whenmixing C and C++ code. New code should prefer to use filebufs. See stdiobuf.

strstreambuf

This buffer class stores and fetches characters from arrays of bytes in memory (i.e.,strings). See sstreambuf.

Classes derived from istream, ostream, and iostream

Classes derived from istream, ostream, and iostream specialize the core classes for use with particular kinds of streambufs. These classes are:

ifstream
ofstream
fstream

These classes support formatted I/O to and from files. They use a filebuf to do the I/O. Common operations (such as opening and closing) can be done directly on streams without explicit mention of filebufs. See fstream.

fstreambase

The member functions common to all three classes are defined in class fstreambase.

istrstream
ostrstream
strstream

These classes support the processing of arrays of bytes (e.g. strings), and use class strstreambuf (see strstream).

strstreambase

The member functions common to these classes are defined in class strstreambase.

stdiostream

This class specializes iostream for stdio FILEs (see stdiobuf).

BUGS

Performance of programs that copy from cin to cout can sometimes be improved by breaking the tie between cin and cout and doing explicit flushes of cout.
Some member functions of streambuf and ios (not discussed in this section) are present only for backward compatibility with the stream package.

SEE ALSO


filebuf , fstream, ios, istreammanipostream, sbufprot, sbufpub, strstream, sstreambu), stdiobuf