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.

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 > variable;
7,
cout > variable;
8
cout > variable;
9 and
cout  variable;
36 values are displayed as 0 or 1 by default, instead of
cout > variable;
07 or
cout > 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 > variable;
41] and formatting flags [
cout > variable;
42]; and provides member functions [such as
cout > variable;
43,
cout > 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 > variable;
46]. They are:

  • cout > 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 > 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 > 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 > 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 > variable;
7, all the other IO manipulators are sticky, i.e., they take effect until a new value is set.

Example: Alignment

The

cout > 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 > variable;
43 [e.g.
cout > variable;
58] to set the field width, but
cout > variable;
43 cannot be used with
cout > 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 > 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 > 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 > 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 > variable;
68
cout > variable;
69. The precision is interpreted differently in default and non-default modes [due to legacy].

  • In default mode [neither
    cout > variable;
    70 nor
    cout > variable;
    71 used], a floating-point number is displayed in fixed-point notation [e.g.,
    cout > variable;
    72] for exponent in the range of
    cout > variable;
    73; and scientific notation [e.g.,
    cout > 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 > variable;
    75 to show or hide the trailing zeros.
  • In both
    cout > variable;
    70 [e.g.,
    cout > variable;
    72] and
    cout > variable;
    71 [e.g.,
    cout > 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 > variable;
81 [e.g.
cout > variable;
82] to set the floating-point precision, but
cout > variable;
62 cannot be used with
cout > 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 > variable;
    87: Set the integral number base. Negative
    cout > variable;
    88 and
    cout > variable;
    89 are displayed in 2's complement format. Alternatively, you can use
    cout > 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 > variable;
    92: write
    cout > variable;
    88 values with
    cout > variable;
    94 prefix; and
    cout > variable;
    89 values with
    cout > variable;
    96 prefix.
  • cout > variable;
    97: write positive
    cout > variable;
    98 value with + sign.
  • cout > 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 > variable;
    31: read/write bool value as alphabetic string
    cout > variable;
    07 or
    cout > variable;
    38.
Other manipulators
  • cout 

Chủ Đề