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: #include <iostream.h> typedef long streampos, streamoff; class streambuf; extern istream_withassign cin; #include <fstream.h> #include <strstream.h> #include <stdiostream.h> #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 classesThe 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 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 streamsThe 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 streambufClasses 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 iostreamClasses derived from istream, ostream, and iostream specialize the core classes for use with particular kinds of streambufs. These classes are: ifstream 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 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. |
SEE ALSO | |
filebuf , fstream, ios, istream, manip, ostream, sbufprot, sbufpub, strstream, sstreambu), stdiobuf |