When a C++ program begins execution, four built-in streams are automatically opened. They are listed here:
|
Stream |
Meaning |
Default Device |
|---|---|---|
|
cin |
Standard input |
Keyboard |
|
cout |
Standard output |
Screen |
|
cerr |
Standard error output |
Screen |
|
clog |
Buffered version of cerr |
Screen |
By default, the standard streams are used to communicate with the console. However, in environments that support I/O redirection (such as DOS, UNIX, and Windows), the standard streams can be redirected to other devices or files.
In the C++ I/O system, each stream has associated with it a set of format flags that control the way information is formatted by a stream. In ios are defined the following enumerated values. These values are used to set or clear the format flags.
|
adjustfield |
basefield |
dec |
fixed |
|
floatfield |
hex |
internal |
left |
|
oct |
right |
scientific |
showbase |
|
showpoint |
showpos |
skipws |
stdio |
|
unitbuf |
uppercase |
Since these flags are defined within the ios class, you will need to explicitly specify this when using them in a program. For example, to refer to left you will write ios::left .
When the
skipws
flag is set, leading whitespace
When the
left
flag is set, output is left-justified. When
right
is set, output is right-justified. When the
internal
flag is set, a numeric value is
By default, numeric values are output in decimal. However, it is possible to change the number base. Setting the oct flag causes output to be displayed in octal. Setting the hex flag causes output to be displayed in hexadecimal. To return output to decimal, set the dec flag.
Setting showbase causes the base of numeric values to be shown. For example, if the conversion base is hexadecimal, the value 1F will be displayed as 0x1F.
By default, when scientific notation is displayed, the e is in lowercase. Also, when a hexadecimal value is displayed, the x is in lowercase. When uppercas e is set, these characters are displayed in uppercase.
Setting showpos causes a leading plus sign to be displayed before positive values.
Setting showpoint causes a decimal point and trailing zeros to be displayed for all floating-point output—whether needed or not.
By setting the
scientific
flag, floating-point numeric values are displayed using scientific notation. When fixed is set, floating-point values are displayed using normal notation. When
When unitbuf is set, the buffer is flushed after each insertion operation.
When
stdio
is set,
Since it is common to refer to the oct , dec , and hex fields, they can be collectively referred to as ios::basefield . Similarly, the left , right , and internal fields can be referred to as ios::adjustfield . Finally, the scientific and fixed fields can be referenced as ios::floatfield .
The format flags are typically stored in a long integer and may be set by various member functions of the ios class.
In addition to setting or clearing the format flags directly, there is another way in which you may alter the format parameters of a stream. This second way is through the use of special functions called
|
Manipulator |
Purpose |
Input/Output |
|---|---|---|
|
dec |
Use decimal integers |
Input/output |
|
endl |
Output a newline character and flush the stream |
Output |
|
ends |
Output a null |
Output |
|
flush |
Flush a stream |
Output |
|
hex |
Use hexadecimal integers |
Input/output |
|
oct |
Use octal integers |
Input/output |
|
resetiosflags (long f) |
|
Input/output |
|
setbase(int base) |
Set the number base to base |
Output |
|
setfill(int ch) |
Set the fill character to ch |
Output |
|
setiosflags (long f) |
Turn on the flags specified in f |
Input/output |
|
setprecision (int p) |
Set the number of digits of precision |
Output |
|
setw(int w) |
Set the field width to w |
Output |
|
ws |
Skip leading whitespace |
Input |
To access manipulators that take parameters, such as setw( ) , you must include iomanip.h in your program.