iostream
FileThe business of managing streams and buffers can get a bit complicated, but including the iostream
(formerly iostream.h
) file brings in several classes designed to implement and manage streams and buffers for you. The C++98 version of C++ I/O defines class templates in order to support both char
and wchar_t
data. C++11 adds char16_t
and char32_t
specializations. By using the typedef
facility, C++ makes the char
specializations of these templates mimic the traditional non-template I/O implementation. Here are some of those classes (see Figure 17.3):
• The streambuf
class provides memory for a buffer, along with class methods for filling the buffer, accessing buffer contents, flushing the buffer, and managing the buffer memory.
• The ios_base
class represents general properties of a stream, such as whether it’s open for reading and whether it’s a binary or a text stream.
• The ios
class is based on ios_base
, and it includes a pointer member to a streambuf
object.
• The ostream
class derives from the ios
class and provides output methods.
• The istream
class derives from the ios
class and provides input methods.
• The iostream
class is based on the istream
and ostream
classes and thus inherits both input and output methods.
To use these facilities, you use objects of the appropriate classes. For example, you use an ostream
object such as cout
to handle output. Creating such an object opens a stream, automatically creates a buffer, and associates it with the stream. It also makes the class member functions available to you.
The C++ iostream
class library takes care of many details for you. For example, including the iostream
file in a program creates eight stream objects (four for narrow character streams and four for wide character streams) automatically:
• The cin
object corresponds to the standard input stream. By default, this stream is associated with the standard input device, typically a keyboard. The wcin
object is similar but works with the wchar_t
type.
• The cout
object corresponds to the standard output stream. By default, this stream is associated with the standard output device, typically a monitor. The wcout
object is similar but works with the wchar_t
type.
• The cerr
object corresponds to the standard error stream, which you can use for displaying error messages. By default, this stream is associated with the standard output device, typically a monitor, and the stream is unbuffered. This means that information is sent directly to the screen, without waiting for a buffer to fill or for a newline character. The wcerr
object is similar but works with the wchar_t
type.
• The clog
object also corresponds to the standard error stream. By default, this stream is associated with the standard output device, typically a monitor, and the stream is buffered. The wclog
object is similar but works with the wchar_t
type.
What does it mean to say that an object represents a stream? Well, for example, when the iostream
file declares a cout
object for a program, that object has data members holding information relating to output, such as the field widths to be used in displaying data, the number of places after the decimal to use, what number base to use for displaying integers, and the address of a streambuf
object that describes the buffer used to handle the output flow. A statement such as the following places the characters from the string "Bjarne free"
into the buffer managed by cout
via the pointed-to streambuf
object:
cout << "Bjarne free";
The ostream
class defines the operator<<()
function used in this statement, and the ostream
class also supports the cout
data members with a variety of other class methods, such as the ones this chapter discusses later. Furthermore, C++ sees to it that the output from the buffer is directed to the standard output, usually a monitor, provided by the operating system. In short, one end of a stream is connected to the program, the other end is connected to the standard output, and the cout
object, with the help of a type streambuf
object, manages the flow of bytes through the stream.
3.139.97.40