The ANSI/ISO Standard C++ I/O Classes

The ANSI/ISO Standard C++ I/O Classes

C++ I/O Classes

As explained in Chapter 14, there are two versions of C++’s iostream library: the old style defined by early versions of C++, and the modern approach as defined by the ANSI/ISO Standard for C++ (Standard C++). The old-style library was described in the preceding section. The Standard C++ iostream library is described here.

Using the Standard C++ IostreamLibrary

There are two fundamental differences between the old-style and the Standard C++ iostream libraries. First, the old-style library was defined in the global namespace. The Standard C++ iostream library is contained in the std namespace. Second, the old-style library uses C-like .h header files. The Standard C++ library uses C++ headers (which don’t use the .h).
To use the Standard C++ iostream library, include the header <iostream> in your program. After doing that, you will usually want to bring the library into your current namespace using a statement like this:
using namespace std;
After the using statement, both the old-style and modern libraries work in much the same way.
It is not necessary to use the using statement just described. Instead, you can include an explicit namespace qualifier each time you refer to a member of the I/O classes. For example, the following explicitly refers to cout:
std::cout << "This is a test";
Of course, if you will be making extensive use of the iostream library, then including the using statement makes things less tedious.

The Standard C++ I/O Classes

The Standard C++ I/O system is constructed from a rather complex system of template classes. These classes are shown here:
Class
Purpose
basic_ios
Provides general-purpose I/O operations
basic_streambuf
Low-level support for I/O
basic_istream
Support for input operations
basic_ostream
Support for output operations
basic_iostream
Support for input/output operations
basic_filebuf
Low-level support for file I/O
basic_ifstream
Support for file input
basic_ofstream
Support for file output
basic_fstream
Support for file input/output
basic_stringbuf
Low-level support for string-based I/O
basic_istringstream
Support for string-based input
basic_ostringstream
Support for string-based output
basic_stringstream
Support for string-based input/output
Also part of the I/O class hierarchy is the nontemplate class ios_base. It provides definitions for various elements of the I/O system.
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. The classes basic_filebuf and basic_stringbuf are derived from basic_streambuf. Unless you are doing advanced I/O programming, you will not need to use basic_streambuf or its subclasses 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. 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. Specifically, from basic_istream are derived the classes basic_ifstream and basic_istringstream, from basic_ostream are derived basic_ofstream and basic_ostringstream, and from basic_iostream are derived basic_fstream and basic_stringstream. A base class for basic_ios is ios_base. Thus, any class derived from basic_ios has access to the members of ios_base.
The I/O classes are parameterized for the type of characters that they act upon and for the traits associated with those characters. For example, here is the template specification for basic_ios:
template <class CharType, class Attr = char_traits<CharType> >
   class basic_ios: public ios_base
Here, CharType specifies the type of character (such as char or wchar_t) and Attr specifies a type that describes its attributes. The generic type char_traits is a utility class that defines the attributes associated with a character.
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 complete list of the mapping of template class names to their character and wide-character versions:
Template Class
Character-Based Class
Wide-Character-Based Class
basic_ios
ios
wios
basic_istream
istream
wistream
basic_ostream
ostream
wostream
basic_iostream
iostream
wiostream
basic_ifstream
ifstream
wifstream
basic_ofstream
ofstream
wofstream
basic_fstream
fstream
wfstream
basic_istringstream
istringstream
wistringstream
basic_ostringstream
ostringstream
wostringstream
basic_stringstream
stringstream
wstringstream
basic_streambuf
streambuf
wstreambuf
basic_filebuf
filebuf
wfilebuf
basic_stringbuf
stringbuf
wstringbuf
Since the vast majority of programmers will be using character-based I/O, those are the names used by this chapter. Thus, when referring to the I/O classes, we will simply use their character-based names rather then their internal, template names. For instance, this chapter will use the name ios rather than basic_ios, istream rather than basic_istream, and fstream rather than basic_fstream. Remember, parallel classes exist for wide-character streams and they work in the same way as those described here.

C++’s Predefined Streams

The Standard C++ iostream library automatically opens the following streams:
Stream
Meaning
cin
Standard input
cout
Standard output
cerr
Standard error output
clog
Buffered version of cerr
wcin
Wide-character version of cin
wcout
Wide-character version of cout
wcerr
Wide-character version of cerr
wclog
Wide-character version of clog
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 I/O Headers

The Standard C++ I/O system relies upon several headers. They are shown here:
Header
For
<fstream>
File I/O
<iomanip>
Parameterized I/O manipulators
<ios>
Basic I/O support
<iosfwd>
Forward declarations used by the I/O system
<iostream>
General I/O
<istream>
Basic input support
<ostream>
Basic output support
<sstream>
String-based streams
<streambuf>
Low-level I/O support
Several of these headers are used internally by the I/O system. In general, your program will only include <iostream>, <fstream>, <sstream>, or <iomanip>.

The Format Flags

Each stream has associated with it a set of format flags that control the way information is formatted. The ios_base class declares a bitmask enumeration called fmtflags in which the following values are defined:
adjustfield
basefield
boolalpha
dec
fixed
floatfield
hex
internal
left
oct
right
scientific
showbase
showpoint
showpos
skipws
unitbuf
uppercase
These values are used to set or clear the format flags, using functions such as setf( ) and unsetf( ).
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 boolalpha is set, Booleans can be input or output using the keywords true and false.
Since it is common to refer to the oct, dec, and hex fields, they can be collectively referred to as basefield. Similarly, the left, right, and internal fields can be referred to as adjustfield. Finally, the scientific and fixed fields can be referenced as floatfield.

The I/O Manipulators

In addition to setting or clearing the format flags directly, you can alter the format parameters of a stream through the use of special functions called manipulators, which can be included in an I/O expression. The standard manipulators are shown in the following table:
Manipulator
Purpose
Input/Output
boolalpha
Turns on boolapha flag
Input/Output
dec
Turns on dec flag
Input/Ouput
endl
Output a newline character and flush the stream
Output
ends
Output a null
Output
fixed
Turns on fixed flag
Output
flush
Flush a stream
Output
hex
Turns on hex flag
Input/Output
internal
Turns on internal flag
Output
left
Turns on left flag
Output
nobooalpha
Turns off boolalpha flag
Input/Output
noshowbase
Turns off showbase flag
Output
noshowpoint
Turns off showpoint flag
Output
noshowpos
Turns off showpos flag
Output
noskipws
Turns off skipws flag
Input
nounitbuf
Turns off unitbuf flag
Output
nouppercase
Turns off uppercase flag
Output
oct
Turns on oct flag
Input/Output
resetiosflags(fmtflags f)
Turn off the flags specified in f
Input/Output
right
Turns on right flag
Output
scientific
Turns on scientific flag
Output
setbase(int base)
Set the number base to base
Input/Output
setfill(int ch)
Set the fill character to ch
Output
setiosflags(fmtflags 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
showbase
Turns on showbase flag
Output
showpoint
Turns on showpoint flag
Output
showpos
Turns on showpos flag
Output
skipws
Turns on skipws flag
Input
unitbuf
Turns on unitbuf flag
Output
uppercase
Turns on uppercase flag
Output
ws
Skip leading whitespace
Input
To use a manipulator that takes a parameter, you must include <iomanip>.


Programming Tip 
One of the most interesting format flags found in the modern iostream library is boolalpha. This flag can be set either directly of by using the manipulators boolalpha() of noboolalpha(). What makes boolalpha so interesting is that setting it allows you to input and output Boolean values using the keywords true and false. Normally, you must enter 1 for true and zero for false. For example, consider the following program:
// Demonstrate boolalpha format flag.
#include <iostream>
using namespace std;

int main ()
{
  bool b;

  cout << "Before setting boolalpha flag: ";
  b = true;
  cout << b << " ";
  b = false;
  cout << b << endl;

  cout << "After setting boolapha flag: ";
  b = true;
  cout << boolalpha << b << " ";
  b = false;
  cout << b << endl;

  cout << "Enter a Boolean value: ";
  cin >> boolalpha >> b;
  cout << "You entered " << b;

  return 0;
}

Here is a sample ren:
Before setting boolalpha flag: 1 0
After setting boolalpha flag: true false
Enter a Boolean value: true
You entered true
Once the boolalpha flag has been set, Boolean values are input and output using the words true or false. As the program shows you must set boolalpha flag for cin and cout separately. Like all format flags, setting boolalpha for one stream does not imply that it is also set for another.

Followers