The Old-Style C++ I/O System

The Old-Style C++ I/O System

Overview

Because C++ includes the entire C library, it supports the use of C’s I/O system. However, C++ also defines its own class-based, object-oriented I/O system, which is referred to as the iostream library. When writing C++ programs, you will usually want to use the iostream library rather than C-based I/O.
At the time of this writing there are two versions of the iostream library in widespread use: the older one that is based upon the original specifications for C++ and the modern one defined by the ANSI/ISO Standard for C++. Today, most C++ compilers support both the old-style and modern iostream libraries. However, the old-style iostream library is obsolete and should not be used for new code. New code should use the modern approach as defined by the ANSI/ISO C++ Standard. The old-style iostream library is described in this chapter for the benefit of those programmers maintaining old code, or porting old code to the modern standard. The modern approach is described in Chapter 15.
For the most part, both the old-style and modern I/O systems work the same way. If you know how to use one, you can easily use the other. However, there are several important differences between the two.
First, the old-style iostream classes were defined in the global namespace. The modern iostream library is contained in the std namespace.
Second, the modern iostream library is defined using a complex, interrelated set of template classes and functions. The old-style library uses a less complicated, nontemplatized class hierarchy. Fortunately, the names of the classes that you will use in your programs remain the same.
Third, the modern iostream library defines many new data types.
Fourth, to use the old library, you need to include .h header files, such as iostream.h. These header files define the old-style iostream classes and put them into the global namespace. By contrast, to use the modern iostream library, include the new style header <iostream> in your program.
One final point: Since the old-style iostream library is nonstandard, its precise implementation will differ between compilers, and may differ slightly from that described here.

The Basic Stream Classes

The old-style iostream library uses the header file iostream.h. This file defines the foundational class hierarchy that supports I/O operations. If you will be performing file I/O, then you will need to also include fstream.h. To use array-based I/O, you will need to include strstrea.h.
The lowest-level class is called streambuf. This class provides the basic input and output operations. It is used primarily as a base class for other classes. Unless you are deriving your own I/O classes, you will not use streambuf directly.
The class ios is the base of the class hierarchy that you will typically use when using the C++ I/O system. It provides formatting, error checking, and status information. From ios are derived several classes—sometimes through intermediary classes. The classes derived either directly or indirectly from ios that you will typically use are listed here:
Class
Purpose
istream
General input
ostream
General output
iostream
General input/output
ifstream
File input
ofstream
File output
fstream
File input/output
istrstream
Array-based input
ostrstream
Array-based output
strstream
Array-based input/output

C++’s Predefined Streams

When a C++ program begins execution, four built-in streams are automatically opened. They are listed here:
Stream
Meaning
Default Device
cin
Standard input
Keyboard
cout
Standard output
Screen
cerr
Standard error output
Screen
clog
Buffered version of cerr
Screen
By default, the standard streams are used to communicate with the console. However, in environments that support I/O redirection (such as DOS, UNIX, and Windows), the standard streams can be redirected to other devices or files.

The Format Flags

In the C++ I/O system, each stream has associated with it a set of format flags that control the way information is formatted by a stream. In ios are defined the following enumerated values. These values are used to set or clear the format flags.
adjustfield
basefield
dec
fixed
floatfield
hex
internal
left
oct
right
scientific
showbase
showpoint
showpos
skipws
stdio
unitbuf
uppercase
Since these flags are defined within the ios class, you will need to explicitly specify this when using them in a program. For example, to refer to left you will write ios::left.
When the skipws flag is set, leading whitespace characters (spaces, tabs, and newlines) are discarded when performing input on a stream. When skipws is cleared, whitespace characters are not discarded.
When the left flag is set, output is left-justified. When right is set, output is right-justified. When the internal flag is set, a numeric value is padded to fill a field by inserting spaces between any sign or base character. If none of these flags is set, output is right-justified by default.
By default, numeric values are output in decimal. However, it is possible to change the number base. Setting the oct flag causes output to be displayed in octal. Setting the hex flag causes output to be displayed in hexadecimal. To return output to decimal, set the dec flag.
Setting showbase causes the base of numeric values to be shown. For example, if the conversion base is hexadecimal, the value 1F will be displayed as 0x1F.
By default, when scientific notation is displayed, the e is in lowercase. Also, when a hexadecimal value is displayed, the x is in lowercase. When uppercase is set, these characters are displayed in uppercase.
Setting showpos causes a leading plus sign to be displayed before positive values.
Setting showpoint causes a decimal point and trailing zeros to be displayed for all floating-point output—whether needed or not.
By setting the scientific flag, floating-point numeric values are displayed using scientific notation. When fixed is set, floating-point values are displayed using normal notation. When neither flag is set, the compiler chooses an appropriate method.
When unitbuf is set, the buffer is flushed after each insertion operation.
When stdio is set, stdout and stderr are flushed after each output.
Since it is common to refer to the oct, dec, and hex fields, they can be collectively referred to as ios::basefield. Similarly, the left, right, and internal fields can be referred to as ios::adjustfield. Finally, the scientific and fixed fields can be referenced as ios::floatfield.
The format flags are typically stored in a long integer and may be set by various member functions of the ios class.

The I/O Manipulators

In addition to setting or clearing the format flags directly, there is another way in which you may alter the format parameters of a stream. This second way is through the use of special functions called manipulators, which can be included in an I/O expression. The manipulators defined by the old-style iostream library are shown in the following table:
Manipulator
Purpose
Input/Output
dec
Use decimal integers
Input/output
endl
Output a newline character and flush the stream
Output
ends
Output a null
Output
flush
Flush a stream
Output
hex
Use hexadecimal integers
Input/output
oct
Use octal integers
Input/output
resetiosflags (long f)
Turn off the flags specified in f
Input/output
setbase(int base)
Set the number base to base
Output
setfill(int ch)
Set the fill character to ch
Output
setiosflags (long f)
Turn on the flags specified in f
Input/output
setprecision (int p)
Set the number of digits of precision
Output
setw(int w)
Set the field width to w
Output
ws
Skip leading whitespace
Input
To access manipulators that take parameters, such as setw( ), you must include iomanip.h in your program.

bad

#include <iostream.h>int bad() const;
The bad( ) function is a member of ios.
The bad( ) function returns nonzero if a fatal I/O error has occurred in the associated stream; otherwise, zero is returned.

clear

#include <iostream.h>void clear(int flags = 0);
The clear( ) function is a member of ios.
The clear( ) function clears the status flags associated with a stream. If flags is zero (as it is by default), then all error flags are cleared (reset to zero). Otherwise, the status flags will be set to whatever value is specified in flags.

eatwhite

#include <iostream.h>void eatwhite();
The eatwhite( ) function is a member of istream.
The eatwhite( ) function reads and discards all leading whitespace from the associated input stream and advances the get pointer to the first nonwhitespace character.
A related function is ignore( ).

Followers