The setprecision manipulator cannot be used to format data written to a file.

The C language did not build the input/output facilities into the language. In other words, there is no keyword like read or write. Instead, it left the IO to the compiler as external library functions (such as printf and

typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
0 in
typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
1 library). The ANSI C standard formalized these IO functions into Standard IO package (
typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
2). C++ continues this approach and formalizes IO in libraries such as
typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
3 and
typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
4.

Features
  • C++ IO is type safe. IO operations are defined for each of the type. If IO operations are not defined for a particular type, compiler will generate an error.
  • C++ IO operations are based on streams of bytes and are device independent. The same set of operations can be applied to different types of IO devices.

Stream IO

C/C++ IO are based on streams, which are sequence of bytes flowing in and out of the programs (just like water and oil flowing through a pipe). In input operations, data bytes flow from an input source (such as keyboard, file, network or another program) into the program. In output operations, data bytes flow from the program to an output sink (such as console, file, network or another program). Streams acts as an intermediaries between the programs and the actual IO devices, in such the way that frees the programmers from handling the actual devices, so as to archive device independent IO operations.

The setprecision manipulator cannot be used to format data written to a file.

C++ provides both the formatted and unformatted IO functions. In formatted or high-level IO, bytes are grouped and converted to types such as

typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
5,
typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
6, string or user-defined types. In unformatted or low-level IO, bytes are treated as raw bytes and unconverted. Formatted IO operations are supported via overloading the stream insertion (
typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
7) and stream extraction (
typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
8) operators, which presents a consistent public IO interface.

To perform input and output, a C++ program:

  1. Construct a stream object.
  2. Connect (Associate) the stream object to an actual IO device (e.g., keyboard, console, file, network, another program).
  3. Perform input/output operations on the stream, via the functions defined in the stream's pubic interface in a device independent manner. Some functions convert the data between the external format and internal format (formatted IO); while other does not (unformatted or binary IO).
  4. Disconnect (Dissociate) the stream to the actual IO device (e.g., close the file).
  5. Free the stream object.

C++ IO Headers, Templates and Classes

Headers

C++ IO is provided in headers

typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
9 (which included
typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
9,
typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
9,
typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
9 and
typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
9),
typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
9 (for file IO), and
typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
9 (for string IO). Furthermore, the header
typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
9 provided manipulators such as
cout << value;
cin >> variable;
7,
cout << value;
cin >> variable;
8
cout << value;
cin >> variable;
9 and
cout << value1 << value2 << .... ;
cin >> variable1 << variable2 << .... ;
0 for formatting.

The setprecision manipulator cannot be used to format data written to a file.
Template Classes

In order to support various character sets (

cout << value1 << value2 << .... ;
cin >> variable1 << variable2 << .... ;
1 and
cout << value1 << value2 << .... ;
cin >> variable1 << variable2 << .... ;
2 in C++98/03; and
cout << value1 << value2 << .... ;
cin >> variable1 << variable2 << .... ;
3,
cout << value1 << value2 << .... ;
cin >> variable1 << variable2 << .... ;
4 introduced in C++11), the stream classes are written as template classes, which could be instantiated with an actual character type. Most of the template classes take two type parameters. For example,

template  >
class basic_istream;
 
template  >
class basic_ostream;

where:

  • cout << value1 << value2 << .... ;
    cin >> variable1 << variable2 << .... ;
    5 is the character type, such as
    cout << value1 << value2 << .... ;
    cin >> variable1 << variable2 << .... ;
    1 or
    cout << value1 << value2 << .... ;
    cin >> variable1 << variable2 << .... ;
    2;
  • cout << value1 << value2 << .... ;
    cin >> variable1 << variable2 << .... ;
    8, of another template class
    cout << value1 << value2 << .... ;
    cin >> variable1 << variable2 << .... ;
    9, defined the properties of the character operations such as the collating sequence (sorting order) of character set.
Template Instantiations and typedef

As mention, the

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
0 template classes can be instantiated with a character type, such as
cout << value1 << value2 << .... ;
cin >> variable1 << variable2 << .... ;
1 and
cout << value1 << value2 << .... ;
cin >> variable1 << variable2 << .... ;
2. C++ further provides
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
3 statements to name these classes:

typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
Specialization Classes for char type

We shall focus on the specialization classes for

cout << value1 << value2 << .... ;
cin >> variable1 << variable2 << .... ;
1 type:

  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    5 and
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    6: superclasses to maintain common stream properties such as format flag, field width, precision and locale. The superclass
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    5 (which is not a template class) maintains data that is independent of the template parameters; whereas the subclass
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    6 (instantiation of template
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    9) maintains data which is dependent of the template parameters.
  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    0 (
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    1),
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    2 (
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    3): provide the input and output public interfaces.
  • typedef basic_ios           ios;
    typedef basic_ios        wios;
    typedef basic_istream       istream;
    typedef basic_istream    wistream;
    typedef basic_ostream       ostream;
    typedef basic_ostream    wostream;
    typedef basic_iostream      iostream;
    typedef basic_iostream   wiostream;
    typedef basic_streambuf     streambuf;
    typedef basic_streambuf  wstreambuf;
    
    3 (
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    5): subclass of both
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    0 and
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    2, which supports bidirectional input and output operations. Take note that
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    0 and
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    2 are unidirectional streams; whereas
    typedef basic_ios           ios;
    typedef basic_ios        wios;
    typedef basic_istream       istream;
    typedef basic_istream    wistream;
    typedef basic_ostream       ostream;
    typedef basic_ostream    wostream;
    typedef basic_iostream      iostream;
    typedef basic_iostream   wiostream;
    typedef basic_streambuf     streambuf;
    typedef basic_streambuf  wstreambuf;
    
    3 is bidirectional.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    5 template and
    typedef basic_ios           ios;
    typedef basic_ios        wios;
    typedef basic_istream       istream;
    typedef basic_istream    wistream;
    typedef basic_ostream       ostream;
    typedef basic_ostream    wostream;
    typedef basic_iostream      iostream;
    typedef basic_iostream   wiostream;
    typedef basic_streambuf     streambuf;
    typedef basic_streambuf  wstreambuf;
    
    3 class is declared in the
    typedef basic_ios           ios;
    typedef basic_ios        wios;
    typedef basic_istream       istream;
    typedef basic_istream    wistream;
    typedef basic_ostream       ostream;
    typedef basic_ostream    wostream;
    typedef basic_iostream      iostream;
    typedef basic_iostream   wiostream;
    typedef basic_streambuf     streambuf;
    typedef basic_streambuf  wstreambuf;
    
    9 header, not
    typedef basic_ios           ios;
    typedef basic_ios        wios;
    typedef basic_istream       istream;
    typedef basic_istream    wistream;
    typedef basic_ostream       ostream;
    typedef basic_ostream    wostream;
    typedef basic_iostream      iostream;
    typedef basic_iostream   wiostream;
    typedef basic_streambuf     streambuf;
    typedef basic_streambuf  wstreambuf;
    
    9 header.
  • typedef basic_istringstream istringstream;
    typedef basic_ostringstream ostringstream;
    5,
    typedef basic_istringstream istringstream;
    typedef basic_ostringstream ostringstream;
    6 and
    typedef basic_ios           ios;
    typedef basic_ios        wios;
    typedef basic_istream       istream;
    typedef basic_istream    wistream;
    typedef basic_ostream       ostream;
    typedef basic_ostream    wostream;
    typedef basic_iostream      iostream;
    typedef basic_iostream   wiostream;
    typedef basic_streambuf     streambuf;
    typedef basic_streambuf  wstreambuf;
    
    4: for file input, output and bidirectional input/output.
  • typedef basic_istringstream istringstream;
    typedef basic_ostringstream ostringstream;
    8,
    typedef basic_istringstream istringstream;
    typedef basic_ostringstream ostringstream;
    9 and read0: for read1 buffer input, output and bidirectional input/output.
  • read2, read3 and read4: provide memory buffer for the stream, file-stream and string-stream, and the public interface for accessing and managing the buffer.

Buffered IO

[TODO]

TheHeader and the Standard Stream Objects: cin, cout, cerr and clog

The

typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
9 header also included the these headers:
typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
9,
typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
9,
typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
9 and
typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
9. Hence, your program needs to include only the
typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
9 header for IO operations.

The

typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
9 header declares these standard stream objects:

  1. write2 (of
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    0 class,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    1 specialization), write5 (of write6 class,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    1 specialization): corresponding to the standard input stream, defaulted to keyword.
  2. write8 (of
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    2 class), printf0 (of printf1 class): corresponding to the standard output stream, defaulted to the display console.
  3. printf2 (of
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    2 class), printf4 (of printf1 class): corresponding to the standard error stream, defaulted to the display console.
  4. printf6 (of
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    2 class), printf8 (of printf1 class): corresponding to the standard log stream, defaulted to the display console.

The Stream Insertion << and Stream Extraction >> Operators

Formatted output is carried out on streams via the stream insertion

typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
7 and stream extraction
typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
8 operators. For example,

cout << value;
cin >> variable;

Take note that write2/write8 shall be the left operand and the data flow in the direction of the arrows.

The

typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
7 and
typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
8 operators are overloaded to handle fundamental types (such as
typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
5 and
typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
6), and classes (such as read1). You can also overload these operators for your own user-defined types.

The

typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
09 and
typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
10 return a reference to write2 and write8, and thus, support cascading operations. For example,

cout << value1 << value2 << .... ;
cin >> variable1 << variable2 << .... ;

The ostream Class

The

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
2 class is a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
3 to
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
3. It contains two set of output functions: formatted output and unformatted output.

  • The formatted output functions (via overloaded stream insertion operator
    typedef basic_ios           ios;
    typedef basic_ios        wios;
    typedef basic_istream       istream;
    typedef basic_istream    wistream;
    typedef basic_ostream       ostream;
    typedef basic_ostream    wostream;
    typedef basic_iostream      iostream;
    typedef basic_iostream   wiostream;
    typedef basic_streambuf     streambuf;
    typedef basic_streambuf  wstreambuf;
    
    7) convert numeric values (such as
    typedef basic_ios           ios;
    typedef basic_ios        wios;
    typedef basic_istream       istream;
    typedef basic_istream    wistream;
    typedef basic_ostream       ostream;
    typedef basic_ostream    wostream;
    typedef basic_iostream      iostream;
    typedef basic_iostream   wiostream;
    typedef basic_streambuf     streambuf;
    typedef basic_streambuf  wstreambuf;
    
    5,
    typedef basic_ios           ios;
    typedef basic_ios        wios;
    typedef basic_istream       istream;
    typedef basic_istream    wistream;
    typedef basic_ostream       ostream;
    typedef basic_ostream    wostream;
    typedef basic_iostream      iostream;
    typedef basic_iostream   wiostream;
    typedef basic_streambuf     streambuf;
    typedef basic_streambuf  wstreambuf;
    
    6) from their internal representations (e.g., 16-/32-bit
    typedef basic_ios           ios;
    typedef basic_ios        wios;
    typedef basic_istream       istream;
    typedef basic_istream    wistream;
    typedef basic_ostream       ostream;
    typedef basic_ostream    wostream;
    typedef basic_iostream      iostream;
    typedef basic_iostream   wiostream;
    typedef basic_streambuf     streambuf;
    typedef basic_streambuf  wstreambuf;
    
    5, 64-bit
    typedef basic_ios           ios;
    typedef basic_ios        wios;
    typedef basic_istream       istream;
    typedef basic_istream    wistream;
    typedef basic_ostream       ostream;
    typedef basic_ostream    wostream;
    typedef basic_iostream      iostream;
    typedef basic_iostream   wiostream;
    typedef basic_streambuf     streambuf;
    typedef basic_streambuf  wstreambuf;
    
    6) to a stream of characters that representing the numeric values in text form.
  • The unformatted output functions (e.g.,
    typedef basic_ios           ios;
    typedef basic_ios        wios;
    typedef basic_istream       istream;
    typedef basic_istream    wistream;
    typedef basic_ostream       ostream;
    typedef basic_ostream    wostream;
    typedef basic_iostream      iostream;
    typedef basic_iostream   wiostream;
    typedef basic_streambuf     streambuf;
    typedef basic_streambuf  wstreambuf;
    
    21,
    typedef basic_ios           ios;
    typedef basic_ios        wios;
    typedef basic_istream       istream;
    typedef basic_istream    wistream;
    typedef basic_ostream       ostream;
    typedef basic_ostream    wostream;
    typedef basic_iostream      iostream;
    typedef basic_iostream   wiostream;
    typedef basic_streambuf     streambuf;
    typedef basic_streambuf  wstreambuf;
    
    22) outputs the bytes as they are, without format conversion.
Formatting Output via the Overloaded Stream Insertion << Operator

The

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
2 class overloads the stream insertion
typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
7 operator for each of the C++ fundamental types (
cout << value1 << value2 << .... ;
cin >> variable1 << variable2 << .... ;
1,
typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
26,
typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
27,
typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
28,
typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
29,
typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
5,
typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
31,
typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
32,
typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
33,
typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
34 (C++11),
typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
35 (C++11),
typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
36,
typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
6 and
typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
38. It converts a numeric value from its internal representation to the text form.

The

typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
7 operator returns a reference to the invoking
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
2 object. Hence, you can concatenate
typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
7 operations, e.g.,
typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
42.

The

typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
7 operator is also overloaded for the following pointer types:

  • typedef basic_ios           ios;
    typedef basic_ios        wios;
    typedef basic_istream       istream;
    typedef basic_istream    wistream;
    typedef basic_ostream       ostream;
    typedef basic_ostream    wostream;
    typedef basic_iostream      iostream;
    typedef basic_iostream   wiostream;
    typedef basic_streambuf     streambuf;
    typedef basic_streambuf  wstreambuf;
    
    44,
    typedef basic_ios           ios;
    typedef basic_ios        wios;
    typedef basic_istream       istream;
    typedef basic_istream    wistream;
    typedef basic_ostream       ostream;
    typedef basic_ostream    wostream;
    typedef basic_iostream      iostream;
    typedef basic_iostream   wiostream;
    typedef basic_streambuf     streambuf;
    typedef basic_streambuf  wstreambuf;
    
    45,
    typedef basic_ios           ios;
    typedef basic_ios        wios;
    typedef basic_istream       istream;
    typedef basic_istream    wistream;
    typedef basic_ostream       ostream;
    typedef basic_ostream    wostream;
    typedef basic_iostream      iostream;
    typedef basic_iostream   wiostream;
    typedef basic_streambuf     streambuf;
    typedef basic_streambuf  wstreambuf;
    
    46: for outputting C-strings and literals. It uses the terminating null character to decide the end of the
    cout << value1 << value2 << .... ;
    cin >> variable1 << variable2 << .... ;
    1 array.
  • typedef basic_ios           ios;
    typedef basic_ios        wios;
    typedef basic_istream       istream;
    typedef basic_istream    wistream;
    typedef basic_ostream       ostream;
    typedef basic_ostream    wostream;
    typedef basic_iostream      iostream;
    typedef basic_iostream   wiostream;
    typedef basic_streambuf     streambuf;
    typedef basic_streambuf  wstreambuf;
    
    48: can be used to print an address.

For example,

Flushing the Output Buffer

You can flush the output buffer via:

  1. typedef basic_ios           ios;
    typedef basic_ios        wios;
    typedef basic_istream       istream;
    typedef basic_istream    wistream;
    typedef basic_ostream       ostream;
    typedef basic_ostream    wostream;
    typedef basic_iostream      iostream;
    typedef basic_iostream   wiostream;
    typedef basic_streambuf     streambuf;
    typedef basic_streambuf  wstreambuf;
    
    49 member function or manipulator:
  2. typedef basic_ios           ios;
    typedef basic_ios        wios;
    typedef basic_istream       istream;
    typedef basic_istream    wistream;
    typedef basic_ostream       ostream;
    typedef basic_ostream    wostream;
    typedef basic_iostream      iostream;
    typedef basic_iostream   wiostream;
    typedef basic_streambuf     streambuf;
    typedef basic_streambuf  wstreambuf;
    
    50 manipulator, which inserts a newline and flush the buffer. Outputting a newline character
    typedef basic_ios           ios;
    typedef basic_ios        wios;
    typedef basic_istream       istream;
    typedef basic_istream    wistream;
    typedef basic_ostream       ostream;
    typedef basic_ostream    wostream;
    typedef basic_iostream      iostream;
    typedef basic_iostream   wiostream;
    typedef basic_streambuf     streambuf;
    typedef basic_streambuf  wstreambuf;
    
    51 may not flush the output buffer; but
    typedef basic_ios           ios;
    typedef basic_ios        wios;
    typedef basic_istream       istream;
    typedef basic_istream    wistream;
    typedef basic_ostream       ostream;
    typedef basic_ostream    wostream;
    typedef basic_iostream      iostream;
    typedef basic_iostream   wiostream;
    typedef basic_streambuf     streambuf;
    typedef basic_streambuf  wstreambuf;
    
    50 does.
  3. write2: output buffer is flushed when input is pending, e.g.,

The istream class

Similar to the

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
2 class, the
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
0 class is a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
3 to
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
1. It also supports formatted input and unformatted input.

  • In formatting input, via overloading the
    typedef basic_ios           ios;
    typedef basic_ios        wios;
    typedef basic_istream       istream;
    typedef basic_istream    wistream;
    typedef basic_ostream       ostream;
    typedef basic_ostream    wostream;
    typedef basic_iostream      iostream;
    typedef basic_iostream   wiostream;
    typedef basic_streambuf     streambuf;
    typedef basic_streambuf  wstreambuf;
    
    8 extraction operator, it converts the text form (a stream of character) into internal representation (such as 16-/32-bit
    typedef basic_ios           ios;
    typedef basic_ios        wios;
    typedef basic_istream       istream;
    typedef basic_istream    wistream;
    typedef basic_ostream       ostream;
    typedef basic_ostream    wostream;
    typedef basic_iostream      iostream;
    typedef basic_iostream   wiostream;
    typedef basic_streambuf     streambuf;
    typedef basic_streambuf  wstreambuf;
    
    5, 64-byte
    typedef basic_ios           ios;
    typedef basic_ios        wios;
    typedef basic_istream       istream;
    typedef basic_istream    wistream;
    typedef basic_ostream       ostream;
    typedef basic_ostream    wostream;
    typedef basic_iostream      iostream;
    typedef basic_iostream   wiostream;
    typedef basic_streambuf     streambuf;
    typedef basic_streambuf  wstreambuf;
    
    6).
  • In unformatting input, such as
    typedef basic_ios           ios;
    typedef basic_ios        wios;
    typedef basic_istream       istream;
    typedef basic_istream    wistream;
    typedef basic_ostream       ostream;
    typedef basic_ostream    wostream;
    typedef basic_iostream      iostream;
    typedef basic_iostream   wiostream;
    typedef basic_streambuf     streambuf;
    typedef basic_streambuf  wstreambuf;
    
    61,
    typedef basic_ios           ios;
    typedef basic_ios        wios;
    typedef basic_istream       istream;
    typedef basic_istream    wistream;
    typedef basic_ostream       ostream;
    typedef basic_ostream    wostream;
    typedef basic_iostream      iostream;
    typedef basic_iostream   wiostream;
    typedef basic_streambuf     streambuf;
    typedef basic_streambuf  wstreambuf;
    
    62,
    typedef basic_ios           ios;
    typedef basic_ios        wios;
    typedef basic_istream       istream;
    typedef basic_istream    wistream;
    typedef basic_ostream       ostream;
    typedef basic_ostream    wostream;
    typedef basic_iostream      iostream;
    typedef basic_iostream   wiostream;
    typedef basic_streambuf     streambuf;
    typedef basic_streambuf  wstreambuf;
    
    63, it reads the characters as they are, without conversion.
Formatting Input via the Overloaded Stream Extraction >> Operator

The

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
0 class overloads the extraction
typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
8 operator for each of the C++ fundamental types (
cout << value1 << value2 << .... ;
cin >> variable1 << variable2 << .... ;
1,
typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
26,
typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
27,
typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
28,
typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
29,
typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
5,
typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
31,
typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
32,
typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
33,
typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
34 (C++11),
typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
35 (C++11),
typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
36,
typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
6 and
typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
38. It performs formatting by converting the input texts into the internal representation of the respective types.

The

typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
8 operator returns a reference to the invokind
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
0 object. Hence, you can concatenate
typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
8 operations, e.g.,
typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
83.

The

typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
8 operator is also overloaded for the following pointer types:

  • typedef basic_ios           ios;
    typedef basic_ios        wios;
    typedef basic_istream       istream;
    typedef basic_istream    wistream;
    typedef basic_ostream       ostream;
    typedef basic_ostream    wostream;
    typedef basic_iostream      iostream;
    typedef basic_iostream   wiostream;
    typedef basic_streambuf     streambuf;
    typedef basic_streambuf  wstreambuf;
    
    44,
    typedef basic_ios           ios;
    typedef basic_ios        wios;
    typedef basic_istream       istream;
    typedef basic_istream    wistream;
    typedef basic_ostream       ostream;
    typedef basic_ostream    wostream;
    typedef basic_iostream      iostream;
    typedef basic_iostream   wiostream;
    typedef basic_streambuf     streambuf;
    typedef basic_streambuf  wstreambuf;
    
    45,
    typedef basic_ios           ios;
    typedef basic_ios        wios;
    typedef basic_istream       istream;
    typedef basic_istream    wistream;
    typedef basic_ostream       ostream;
    typedef basic_ostream    wostream;
    typedef basic_iostream      iostream;
    typedef basic_iostream   wiostream;
    typedef basic_streambuf     streambuf;
    typedef basic_streambuf  wstreambuf;
    
    46: for inputting C-strings. It uses whitespace as delimiter and adds a terminating null character to the C-string.

[TODO] Read "C-string input".

Flushing the Input Buffer - ignore()

You can use the

typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
88 to discard characters in the input buffer:

Unformatted Input/Output Functions

put(), get() and getline()

The

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
2's member function
typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
21 can be used to put out a
cout << value1 << value2 << .... ;
cin >> variable1 << variable2 << .... ;
1.
typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
21 returns the invoking
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
2 reference, and thus, can be cascaded. For example,

[TODO] Example

read(), write() and gcount()
Other istream functions - peek() and putback()

States of stream

The steam superclass

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
5 maintains a data member to describe the states of the stream, which is a bitmask of the type
typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
95. The flags are:

  • typedef basic_ios           ios;
    typedef basic_ios        wios;
    typedef basic_istream       istream;
    typedef basic_istream    wistream;
    typedef basic_ostream       ostream;
    typedef basic_ostream    wostream;
    typedef basic_iostream      iostream;
    typedef basic_iostream   wiostream;
    typedef basic_streambuf     streambuf;
    typedef basic_streambuf  wstreambuf;
    
    96: set when an input operation reaches end-of-file.
  • typedef basic_ios           ios;
    typedef basic_ios        wios;
    typedef basic_istream       istream;
    typedef basic_istream    wistream;
    typedef basic_ostream       ostream;
    typedef basic_ostream    wostream;
    typedef basic_iostream      iostream;
    typedef basic_iostream   wiostream;
    typedef basic_streambuf     streambuf;
    typedef basic_streambuf  wstreambuf;
    
    97: The last input operation failed to read the expected characters or output operation failed to write the expected characters, e.g.,
    typedef basic_ios           ios;
    typedef basic_ios        wios;
    typedef basic_istream       istream;
    typedef basic_istream    wistream;
    typedef basic_ostream       ostream;
    typedef basic_ostream    wostream;
    typedef basic_iostream      iostream;
    typedef basic_iostream   wiostream;
    typedef basic_streambuf     streambuf;
    typedef basic_streambuf  wstreambuf;
    
    98 reads n characters without reaching delimiter character.
  • typedef basic_ios           ios;
    typedef basic_ios        wios;
    typedef basic_istream       istream;
    typedef basic_istream    wistream;
    typedef basic_ostream       ostream;
    typedef basic_ostream    wostream;
    typedef basic_iostream      iostream;
    typedef basic_iostream   wiostream;
    typedef basic_streambuf     streambuf;
    typedef basic_streambuf  wstreambuf;
    
    99: serious error due to failure of an IO operation (e.g. file read/write error) or stream buffer.
  • cout << value;
    cin >> variable;
    00: Absence of above error with value of 0.

These flags are defined as public static members in

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
5. They can be accessed directly via
cout << value;
cin >> variable;
02 or via subclasses such as
cout << value;
cin >> variable;
03,
cout << value;
cin >> variable;
04. However, it is more convenience to use these public member functions of
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
6 class:

  • cout << value;
    cin >> variable;
    06: returns
    cout << value;
    cin >> variable;
    07 if
    cout << value;
    cin >> variable;
    00 is set (i.e., no error).
  • cout << value;
    cin >> variable;
    09: returns
    cout << value;
    cin >> variable;
    07 if
    typedef basic_ios           ios;
    typedef basic_ios        wios;
    typedef basic_istream       istream;
    typedef basic_istream    wistream;
    typedef basic_ostream       ostream;
    typedef basic_ostream    wostream;
    typedef basic_iostream      iostream;
    typedef basic_iostream   wiostream;
    typedef basic_streambuf     streambuf;
    typedef basic_streambuf  wstreambuf;
    
    96 is set.
  • cout << value;
    cin >> variable;
    12: returns
    cout << value;
    cin >> variable;
    07 if
    typedef basic_ios           ios;
    typedef basic_ios        wios;
    typedef basic_istream       istream;
    typedef basic_istream    wistream;
    typedef basic_ostream       ostream;
    typedef basic_ostream    wostream;
    typedef basic_iostream      iostream;
    typedef basic_iostream   wiostream;
    typedef basic_streambuf     streambuf;
    typedef basic_streambuf  wstreambuf;
    
    97 or
    typedef basic_ios           ios;
    typedef basic_ios        wios;
    typedef basic_istream       istream;
    typedef basic_istream    wistream;
    typedef basic_ostream       ostream;
    typedef basic_ostream    wostream;
    typedef basic_iostream      iostream;
    typedef basic_iostream   wiostream;
    typedef basic_streambuf     streambuf;
    typedef basic_streambuf  wstreambuf;
    
    99 is set.
  • cout << value;
    cin >> variable;
    16: returns
    cout << value;
    cin >> variable;
    07 if
    typedef basic_ios           ios;
    typedef basic_ios        wios;
    typedef basic_istream       istream;
    typedef basic_istream    wistream;
    typedef basic_ostream       ostream;
    typedef basic_ostream    wostream;
    typedef basic_iostream      iostream;
    typedef basic_iostream   wiostream;
    typedef basic_streambuf     streambuf;
    typedef basic_streambuf  wstreambuf;
    
    99 is set.
  • cout << value;
    cin >> variable;
    19: clear
    typedef basic_ios           ios;
    typedef basic_ios        wios;
    typedef basic_istream       istream;
    typedef basic_istream    wistream;
    typedef basic_ostream       ostream;
    typedef basic_ostream    wostream;
    typedef basic_iostream      iostream;
    typedef basic_iostream   wiostream;
    typedef basic_streambuf     streambuf;
    typedef basic_streambuf  wstreambuf;
    
    96,
    typedef basic_ios           ios;
    typedef basic_ios        wios;
    typedef basic_istream       istream;
    typedef basic_istream    wistream;
    typedef basic_ostream       ostream;
    typedef basic_ostream    wostream;
    typedef basic_iostream      iostream;
    typedef basic_iostream   wiostream;
    typedef basic_streambuf     streambuf;
    typedef basic_streambuf  wstreambuf;
    
    97 and
    typedef basic_ios           ios;
    typedef basic_ios        wios;
    typedef basic_istream       istream;
    typedef basic_istream    wistream;
    typedef basic_ostream       ostream;
    typedef basic_ostream    wostream;
    typedef basic_iostream      iostream;
    typedef basic_iostream   wiostream;
    typedef basic_streambuf     streambuf;
    typedef basic_streambuf  wstreambuf;
    
    99.

Formatting Input/Output via Manipulators inand

C++ provides a set of manipulators to perform input and output formatting:

  1. typedef basic_ios           ios;
    typedef basic_ios        wios;
    typedef basic_istream       istream;
    typedef basic_istream    wistream;
    typedef basic_ostream       ostream;
    typedef basic_ostream    wostream;
    typedef basic_iostream      iostream;
    typedef basic_iostream   wiostream;
    typedef basic_streambuf     streambuf;
    typedef basic_streambuf  wstreambuf;
    
    9 header:
    cout << value;
    cin >> variable;
    7,
    cout << value;
    cin >> variable;
    8,
    cout << value;
    cin >> variable;
    26,
    cout << value;
    cin >> variable;
    9.
  2. typedef basic_ios           ios;
    typedef basic_ios        wios;
    typedef basic_istream       istream;
    typedef basic_istream    wistream;
    typedef basic_ostream       ostream;
    typedef basic_ostream    wostream;
    typedef basic_iostream      iostream;
    typedef basic_iostream   wiostream;
    typedef basic_streambuf     streambuf;
    typedef basic_streambuf  wstreambuf;
    
    9 header:
    cout << value;
    cin >> variable;
    29,
    cout << value;
    cin >> variable;
    30,
    cout << value;
    cin >> variable;
    31, etc.
Default Output Formatting

The

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
2's
typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
7 stream insertion operator is overloaded to convert a numeric value from its internal representation (e.g., 16-/32-bit
typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
5, 64-bit
typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
6) to the text form.

  • By default, the values are displayed with a field-width just enough to hold the text, without additional leading or trailing spaces. You need to provide spaces between the values, if desired.
  • For integers, all digits will be displayed, by default. For example,
  • For floating-point numbers, the default precison is 6 digits, except that the trailing zeros will not be shown. This default precision (of 6 digits) include all digits before and after the decimal point, but exclude the leading zeros. Scientific notation (E-notation) will be used if the exponent is 6 or more or -5 or less. In scientific notation, the default precision is also 6 digits; the exponent is displayed in 3 digits with plus/minus sign (e.g., +006, -005). For example,
  • cout << value;
    cin >> variable;
    36 values are displayed as 0 or 1 by default, instead of
    cout << value;
    cin >> variable;
    07 or
    cout << value;
    cin >> variable;
    38.
Field Width (setw), Fill Character (setfill) and Alignment (left|right|internal)

The

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
5 superclass (included in
typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
9 header) maintains data members for field-width (
cout << value;
cin >> variable;
41) and formatting flags (
cout << value;
cin >> variable;
42); and provides member functions (such as
cout << value;
cin >> variable;
43,
cout << value;
cin >> variable;
44) for manipulating them.

However, it is more convenience to use the so-called IO manipulators, which returns a reference to the invoking stream object and thus can be concatenated in

typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
7 operator (e.g.,
cout << value;
cin >> variable;
46). They are:

  • cout << value;
    cin >> variable;
    7 manipulator (in
    typedef basic_ios           ios;
    typedef basic_ios        wios;
    typedef basic_istream       istream;
    typedef basic_istream    wistream;
    typedef basic_ostream       ostream;
    typedef basic_ostream    wostream;
    typedef basic_iostream      iostream;
    typedef basic_iostream   wiostream;
    typedef basic_streambuf     streambuf;
    typedef basic_streambuf  wstreambuf;
    
    9 header) to set the field width.
  • cout << value;
    cin >> variable;
    49 manipulator (in
    typedef basic_ios           ios;
    typedef basic_ios        wios;
    typedef basic_istream       istream;
    typedef basic_istream    wistream;
    typedef basic_ostream       ostream;
    typedef basic_ostream    wostream;
    typedef basic_iostream      iostream;
    typedef basic_iostream   wiostream;
    typedef basic_streambuf     streambuf;
    typedef basic_streambuf  wstreambuf;
    
    9 header) to set the fill character
  • cout << value;
    cin >> variable;
    51 manipulator (in
    typedef basic_ios           ios;
    typedef basic_ios        wios;
    typedef basic_istream       istream;
    typedef basic_istream    wistream;
    typedef basic_ostream       ostream;
    typedef basic_ostream    wostream;
    typedef basic_iostream      iostream;
    typedef basic_iostream   wiostream;
    typedef basic_streambuf     streambuf;
    typedef basic_streambuf  wstreambuf;
    
    9 header) to set the text alignment.

The default field-width is 0, i.e., just enough space to display the value. C++ never truncates data, and will expand the field to display the entire value if the field-width is too small. The

cout << value;
cin >> variable;
7 operation is non-sticky. That is, it is applicable only to the next IO operation, and reset back to 0 after the operation. The field-width property is applicable to both output and input operations.

Except

cout << value;
cin >> variable;
7, all the other IO manipulators are sticky, i.e., they take effect until a new value is set.

Example: Alignment

The

cout << value;
cin >> variable;
55 alignment left-align the sign, but right-align the number, as illustrated.

[TODO] Example of field-width for input operations

You can also use

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
2's member function
cout << value;
cin >> variable;
43 (e.g.
cout << value;
cin >> variable;
58) to set the field width, but
cout << value;
cin >> variable;
43 cannot be used with
cout << value;
cin >> variable;
60 operator.

Floating-point Format (fixed|scientific) and Precision (setprecision)

The IO stream superclass

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
5 also maintains data member for the floating-point precision and display format; and provides member functions (such as
cout << value;
cin >> variable;
62) for manipulating them.

Again, it is more convenience to use IO manipulators, which can be concatenated in

typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
7. They are:

  • cout << value;
    cin >> variable;
    8 manipulator (in
    typedef basic_ios           ios;
    typedef basic_ios        wios;
    typedef basic_istream       istream;
    typedef basic_istream    wistream;
    typedef basic_ostream       ostream;
    typedef basic_ostream    wostream;
    typedef basic_iostream      iostream;
    typedef basic_iostream   wiostream;
    typedef basic_streambuf     streambuf;
    typedef basic_streambuf  wstreambuf;
    
    9 header) to set the precision of floating-point number.
  • cout << value;
    cin >> variable;
    66 manipulators (in
    typedef basic_ios           ios;
    typedef basic_ios        wios;
    typedef basic_istream       istream;
    typedef basic_istream    wistream;
    typedef basic_ostream       ostream;
    typedef basic_ostream    wostream;
    typedef basic_iostream      iostream;
    typedef basic_iostream   wiostream;
    typedef basic_streambuf     streambuf;
    typedef basic_streambuf  wstreambuf;
    
    9 header) to set the floating-point display format.

Floating point number can be display in 3 formatting modes:

cout << value;
cin >> variable;
68
cout << value;
cin >> variable;
69. The precision is interpreted differently in default and non-default modes (due to legacy).

  • In default mode (neither
    cout << value;
    cin >> variable;
    70 nor
    cout << value;
    cin >> variable;
    71 used), a floating-point number is displayed in fixed-point notation (e.g.,
    cout << value;
    cin >> variable;
    72) for exponent in the range of
    cout << value;
    cin >> variable;
    73; and scientific notation (e.g.,
    cout << value;
    cin >> variable;
    74) otherwise. The precision in default mode includes digits before and after the decimal point but exclude the leading zeros. Fewer digits might be shown as the trailing zeros are not displayed. The default precision is 6. See the earlier examples for default mode with default precision of 6.
    As mentioned, the trailing zeros are not displayed in default mode, you can use manipulator
    cout << value;
    cin >> variable;
    75 to show or hide the trailing zeros.
  • In both
    cout << value;
    cin >> variable;
    70 (e.g.,
    cout << value;
    cin >> variable;
    72) and
    cout << value;
    cin >> variable;
    71 (e.g.,
    cout << value;
    cin >> variable;
    74), the precision sets the number of digits after decimal point. The default precision is also 6.

For examples,

You can also use

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
2's member function
cout << value;
cin >> variable;
81 (e.g.
cout << value;
cin >> variable;
82) to set the floating-point precision, but
cout << value;
cin >> variable;
62 cannot be used with
cout << value;
cin >> variable;
60 operator.

Integral Number Base (dec|oct|hex, setbase)

C++ support number bases (radixes) of decimal, hexadecimal and octal. You can use the following manipulators (defined in

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
5 class, included in
typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
9 header) to manipulate the integral number base:

  • cout << value;
    cin >> variable;
    87: Set the integral number base. Negative
    cout << value;
    cin >> variable;
    88 and
    cout << value;
    cin >> variable;
    89 are displayed in 2's complement format. Alternatively, you can use
    cout << value;
    cin >> variable;
    90 (in header
    typedef basic_ios           ios;
    typedef basic_ios        wios;
    typedef basic_istream       istream;
    typedef basic_istream    wistream;
    typedef basic_ostream       ostream;
    typedef basic_ostream    wostream;
    typedef basic_iostream      iostream;
    typedef basic_iostream   wiostream;
    typedef basic_streambuf     streambuf;
    typedef basic_streambuf  wstreambuf;
    
    9).
  • cout << value;
    cin >> variable;
    92: write
    cout << value;
    cin >> variable;
    88 values with
    cout << value;
    cin >> variable;
    94 prefix; and
    cout << value;
    cin >> variable;
    89 values with
    cout << value;
    cin >> variable;
    96 prefix.
  • cout << value;
    cin >> variable;
    97: write positive
    cout << value;
    cin >> variable;
    98 value with + sign.
  • cout << value;
    cin >> variable;
    99: write uppercase in certain insertion operations, e.g., hex digits. It does not convert characters or strings to uppercase!

These manipulators are sticky.

For examples,

bool values (boolalpha|noboolalpha)
  • cout << value;
    cin >> variable;
    31: read/write bool value as alphabetic string
    cout << value;
    cin >> variable;
    07 or
    cout << value;
    cin >> variable;
    38.
Other manipulators
  • cout << value1 << value2 << .... ;
    cin >> variable1 << variable2 << .... ;
    03: skip leading white spaces for certain input operations.
  • cout << value1 << value2 << .... ;
    cin >> variable1 << variable2 << .... ;
    04: flush output after each insertion operation.
Notes
  • You need to include the
    typedef basic_ios           ios;
    typedef basic_ios        wios;
    typedef basic_istream       istream;
    typedef basic_istream    wistream;
    typedef basic_ostream       ostream;
    typedef basic_ostream    wostream;
    typedef basic_iostream      iostream;
    typedef basic_iostream   wiostream;
    typedef basic_streambuf     streambuf;
    typedef basic_streambuf  wstreambuf;
    
    9 header for
    cout << value;
    cin >> variable;
    7,
    cout << value;
    cin >> variable;
    8,
    cout << value;
    cin >> variable;
    9, and
    cout << value1 << value2 << .... ;
    cin >> variable1 << variable2 << .... ;
    0.
  • You can use
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    5's (in
    typedef basic_ios           ios;
    typedef basic_ios        wios;
    typedef basic_istream       istream;
    typedef basic_istream    wistream;
    typedef basic_ostream       ostream;
    typedef basic_ostream    wostream;
    typedef basic_iostream      iostream;
    typedef basic_iostream   wiostream;
    typedef basic_streambuf     streambuf;
    typedef basic_streambuf  wstreambuf;
    
    9 header) member functions
    cout << value;
    cin >> variable;
    44 and
    cout << value1 << value2 << .... ;
    cin >> variable1 << variable2 << .... ;
    13 to set the individual formatting flags. However, they are not as user-friendly as using manipulators as discussed above. Furthermore, they cannot be used with
    cout << value;
    cin >> variable;
    60 operator.

The C++ string class Input/Output

[TODO]

File Input/Output (Header)

C++ handles file IO similar to standard IO. In header

typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
9, the class
typedef basic_istringstream istringstream;
typedef basic_ostringstream ostringstream;
6 is a subclass of
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
2;
typedef basic_istringstream istringstream;
typedef basic_ostringstream ostringstream;
5 is a subclass of
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
0; and
typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
4 is a subclass of
typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
3 for bi-directional IO. You need to include both
typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
9 and
typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
9 headers in your program for file IO.

To write to a file, you construct a

cout << value1 << value2 << .... ;
cin >> variable1 << variable2 << .... ;
24 object connecting to the output file, and use the
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
2 functions such as stream insertion
typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
7,
typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
21 and
typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
22. Similarly, to read from an input file, construct an
typedef basic_istringstream istringstream;
typedef basic_ostringstream ostringstream;
5 object connecting to the input file, and use the
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
0 functions such as stream extraction
typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
8,
typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
61,
typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
98 and
typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
63.

File IO requires an additional step to connect the file to the stream (i.e., file open) and disconnect from the stream (i.e., file close).

File Output

The steps are:

  1. Construct an
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    2 object.
  2. Connect it to a file (i.e., file open) and set the mode of file operation (e.g, truncate, append).
  3. Perform output operation via insertion
    typedef basic_ios           ios;
    typedef basic_ios        wios;
    typedef basic_istream       istream;
    typedef basic_istream    wistream;
    typedef basic_ostream       ostream;
    typedef basic_ostream    wostream;
    typedef basic_iostream      iostream;
    typedef basic_iostream   wiostream;
    typedef basic_streambuf     streambuf;
    typedef basic_streambuf  wstreambuf;
    
    8 operator or
    typedef basic_ios           ios;
    typedef basic_ios        wios;
    typedef basic_istream       istream;
    typedef basic_istream    wistream;
    typedef basic_ostream       ostream;
    typedef basic_ostream    wostream;
    typedef basic_iostream      iostream;
    typedef basic_iostream   wiostream;
    typedef basic_streambuf     streambuf;
    typedef basic_streambuf  wstreambuf;
    
    22,
    typedef basic_ios           ios;
    typedef basic_ios        wios;
    typedef basic_istream       istream;
    typedef basic_istream    wistream;
    typedef basic_ostream       ostream;
    typedef basic_ostream    wostream;
    typedef basic_iostream      iostream;
    typedef basic_iostream   wiostream;
    typedef basic_streambuf     streambuf;
    typedef basic_streambuf  wstreambuf;
    
    21 functions.
  4. Disconnect (close the file which flushes the output buffer) and free the
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    2 object.

By default, opening an output file creates a new file if the filename does not exist; or truncates it (clear its content) and starts writing as an empty file.

open(), close() and is_open()
File Modes

File modes are defined as static public member in

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
5 superclass. They can be referenced from
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
5 or its subclasses - we typically use subclass
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
6. The available file mode flags are:

  1. cout << value1 << value2 << .... ;
    cin >> variable1 << variable2 << .... ;
    43 - open file for input operation
  2. cout << value1 << value2 << .... ;
    cin >> variable1 << variable2 << .... ;
    44 - open file for output operation
  3. cout << value1 << value2 << .... ;
    cin >> variable1 << variable2 << .... ;
    45 - output appends at the end of the file.
  4. cout << value1 << value2 << .... ;
    cin >> variable1 << variable2 << .... ;
    46 - truncate the file and discard old contents.
  5. cout << value1 << value2 << .... ;
    cin >> variable1 << variable2 << .... ;
    47 - for binary (raw byte) IO operation, instead of character-based.
  6. cout << value1 << value2 << .... ;
    cin >> variable1 << variable2 << .... ;
    48 - position the file pointer "at the end" for input/output.

You can set multiple flags via bit-or (

cout << value1 << value2 << .... ;
cin >> variable1 << variable2 << .... ;
49) operator, e.g.,
cout << value1 << value2 << .... ;
cin >> variable1 << variable2 << .... ;
50 to append output at the end of the file.

For output, the default is

cout << value1 << value2 << .... ;
cin >> variable1 << variable2 << .... ;
51. For input, the default is
cout << value1 << value2 << .... ;
cin >> variable1 << variable2 << .... ;
52.

The steps are:

  1. Construct an
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    0 object.
  2. Connect it to a file (i.e., file open) and set the mode of file operation.
  3. Perform output operation via extraction
    typedef basic_ios           ios;
    typedef basic_ios        wios;
    typedef basic_istream       istream;
    typedef basic_istream    wistream;
    typedef basic_ostream       ostream;
    typedef basic_ostream    wostream;
    typedef basic_iostream      iostream;
    typedef basic_iostream   wiostream;
    typedef basic_streambuf     streambuf;
    typedef basic_streambuf  wstreambuf;
    
    7 operator or
    typedef basic_ios           ios;
    typedef basic_ios        wios;
    typedef basic_istream       istream;
    typedef basic_istream    wistream;
    typedef basic_ostream       ostream;
    typedef basic_ostream    wostream;
    typedef basic_iostream      iostream;
    typedef basic_iostream   wiostream;
    typedef basic_streambuf     streambuf;
    typedef basic_streambuf  wstreambuf;
    
    63,
    typedef basic_ios           ios;
    typedef basic_ios        wios;
    typedef basic_istream       istream;
    typedef basic_istream    wistream;
    typedef basic_ostream       ostream;
    typedef basic_ostream    wostream;
    typedef basic_iostream      iostream;
    typedef basic_iostream   wiostream;
    typedef basic_streambuf     streambuf;
    typedef basic_streambuf  wstreambuf;
    
    61,
    typedef basic_ios           ios;
    typedef basic_ios        wios;
    typedef basic_istream       istream;
    typedef basic_istream    wistream;
    typedef basic_ostream       ostream;
    typedef basic_ostream    wostream;
    typedef basic_iostream      iostream;
    typedef basic_iostream   wiostream;
    typedef basic_streambuf     streambuf;
    typedef basic_streambuf  wstreambuf;
    
    98 functions.
  4. Disconnect (close the file) and free the
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    0 object.

By default, opening an input file ....

Example on Simple File IO

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

Program Notes:

  • Most of the
    typedef basic_ios           ios;
    typedef basic_ios        wios;
    typedef basic_istream       istream;
    typedef basic_istream    wistream;
    typedef basic_ostream       ostream;
    typedef basic_ostream    wostream;
    typedef basic_iostream      iostream;
    typedef basic_iostream   wiostream;
    typedef basic_streambuf     streambuf;
    typedef basic_streambuf  wstreambuf;
    
    9 functions (such as constructors,
    cout << value1 << value2 << .... ;
    cin >> variable1 << variable2 << .... ;
    60) supports filename in C-string only. You may need to extract the C-string from read1 object via the
    cout << value1 << value2 << .... ;
    cin >> variable1 << variable2 << .... ;
    62 member function.
  • You could use
    cout << value1 << value2 << .... ;
    cin >> variable1 << variable2 << .... ;
    63 to check if the file is opened successfully.
  • The
    cout << value1 << value2 << .... ;
    cin >> variable1 << variable2 << .... ;
    64 function returns a null pointer (converted to
    cout << value;
    cin >> variable;
    38) when it reaches end-of-file.

Binary file, read() and write()

We need to use

typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
63 and
typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
22 member functions for binary file (file mode of
cout << value1 << value2 << .... ;
cin >> variable1 << variable2 << .... ;
68), which read/write raw bytes without interpreting the bytes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

Random Access File

Random access file is associated with a file pointer, which can be moved directly to any location in the file. Random access is crucial in certain applications such as databases and indexes.

You can position the input pointer via

cout << value1 << value2 << .... ;
cin >> variable1 << variable2 << .... ;
69 and output pointer via
cout << value1 << value2 << .... ;
cin >> variable1 << variable2 << .... ;
70. Each of them has two versions: absolute and relative positioning.

Random access file is typically process as binary file, in both input and output modes.

[TODO] Example

String Streams

C++ provides a

typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
9 header, which uses the same public interface to support IO between a program and read1 object (buffer).

The string streams is based on

typedef basic_istringstream istringstream;
typedef basic_ostringstream ostringstream;
9 (subclass of
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
2),
typedef basic_istringstream istringstream;
typedef basic_ostringstream ostringstream;
8 (subclass of
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
0) and bi-directional read0 (subclass of
typedef basic_ios           ios;
typedef basic_ios        wios;
typedef basic_istream       istream;
typedef basic_istream    wistream;
typedef basic_ostream       ostream;
typedef basic_ostream    wostream;
typedef basic_iostream      iostream;
typedef basic_iostream   wiostream;
typedef basic_streambuf     streambuf;
typedef basic_streambuf  wstreambuf;
3).

Which class can be used to create files that can be written to but not read from C++?

Either ofstream or fstream object may be used to open a file for writing. And ifstream object is used to open a file for reading purpose only.

What data type or class is used to open existing files and read data from them into memory?

In C++, files are mainly dealt by using three classes fstream, ifstream, ofstream. fstream: This Stream class can be used for both read and write from/to files.

Which statement opens a file for appending quizlet?

"a" opens a file for appending data to the end of the file. "rb" opens a file for reading binary data. "wb" opens a file for writing binary data.

Which of the following positions the file pointer for a file that has been opened for reading and writing?

Which of the following positions the file pointer for a file that has been opened for reading and writing? Use the seekp(arg) fstream member function with the number of bytes to the record in question (counting the first byte as 0) as argument to position the file pointer.