C++ Streams. McGraw-Hill The Complete Reference Fourth Edition
Mainos
Mainos
C h a p t e r 2 0 : T h e C + + I / O S y s t e m B a s i c s
511
C++ Streams
Like the C-based I/O system, the C++ I/O system operates through streams. Streams were discussed in detail in Chapter 9; that discussion will not be repeated here. However, to summarize: A stream is a logical device that either produces or consumes information.
A stream is linked to a physical device by the I/O system. All streams behave in the same way even though the actual physical devices they are connected to may differ substantially. Because all streams behave the same, the same I/O functions can operate on virtually any type of physical device. For example, you can use the same function that writes to a file to write to the printer or to the screen. The advantage to this approach is that you need learn only one I/O system.
The C++ Stream Classes
As mentioned, Standard C++ provides support for its I/O system in <iostream>. In this header, a rather complicated set of class hierarchies is defined that supports I/O operations. The I/O classes begin with a system of template classes. As explained in
Chapter 18, a template class defines the form of a class without fully specifying the data upon which it will operate. Once a template class has been defined, specific instances of it can be created. As it relates to the I/O library, Standard C++ creates two specializations of the I/O template classes: one for 8-bit characters and another for wide characters. This book will use only the 8-bit character classes since they are by far the most common. But the same techniques apply to both.
The C++ I/O system is built upon two related but different template class hierarchies.
The first is derived from the low-level I/O class called basic_streambuf. This class supplies the basic, low-level input and output operations, and provides the underlying support for the entire C++ I/O system. Unless you are doing advanced I/O programming, you will not need to use basic_streambuf directly. The class hierarchy that you will most commonly be working with is derived from basic_ios. This is a high-level I/O class that provides formatting, error checking, and status information related to stream I/O. (A base class for basic_ios is called ios_base, which defines several nontemplate traits used by basic_ios.) basic_ios is used as a base for several derived classes, including basic_istream, basic_ostream, and basic_iostream. These classes are used to create streams capable of input, output, and input/output, respectively.
As explained, the I/O library creates two specializations of the template class hierarchies just described: one for 8-bit characters and one for wide characters. Here is a list of the mapping of template class names to their character and wide-character versions.
512
C + + : T h e C o m p l e t e R e f e r e n c e
Template Class basic_streambuf basic_ios basic_istream basic_ostream basic_iostream basic_fstream basic_ifstream basic_ofstream
Characterbased Class streambuf ios istream ostream iostream fstream ifstream ofstream
Wide-Characterbased Class wstreambuf wios wistream wostream wiostream wfstream wifstream wofstream
The character-based names will be used throughout the remainder of this book, since they are the names that you will normally use in your programs. They are also the same names that were used by the old I/O library. This is why the old and the new
I/O library are compatible at the source code level.
One last point: The ios class contains many member functions and variables that control or monitor the fundamental operation of a stream. It will be referred to frequently.
Just remember that if you include <iostream> in your program, you will have access to this important class.
C++'s Predefined Streams
When a C++ program begins execution, four built-in streams are automatically opened.
They are:
Stream cin cout cerr clog
Meaning
Standard input
Standard output
Standard error output
Buffered version of cerr
Default Device
Keyboard
Screen
Screen
Screen
Streams cin, cout, and cerr correspond to C's stdin, stdout, and stderr.
By default, the standard streams are used to communicate with the console.
However, in environments that support I/O redirection (such as DOS, Unix, OS/2, and Windows), the standard streams can be redirected to other devices or files. For the sake of simplicity, the examples in this chapter assume that no I/O redirection has occurred.
Lataa
Mainos