Notes on Stream IO

samplecopy.cpp an example of using streambuf_iterator's and the copy algorithm, to copy files.

Format Manipulators

For convenience, manipulators provide a way to change certain properties of streams, or otherwise affect them, in the middle of expressions involving `<<' or `>>'. For example, you might write

cout << "|" << setfill('*') << setw(5) << 234 << "|";

to produce `|**234|' as output.

Manipulator: ws

Skip whitespace.

Manipulator: flush

Flush an output stream. For example, `cout << ... <Manipulator: endl

Write an end of line character `\n', then flushes the output stream.

Manipulator: ends

Write `\0' (the string terminator character).

Manipulator: setprecision (int signif)

You can change the value of ios::precision in `<<' expressions with the manipulator `setprecision(signif)'; for example,

cout << setprecision(2) << 4.567;

prints `4.6'. Requires `#include '.

Manipulator: setw (int n)

You can change the value of ios::width in `<<' expressions with the manipulator `setw(n)'; for example,

cout << setw(5) << 234;

prints ` 234' with two leading blanks. Requires `#include '.

Manipulator: setbase (int base)

Where base is one of 10 (decimal), 8 (octal), or 16 (hexadecimal), change the base value for numeric representations. Requires `#include '.

Manipulator: dec

Select decimal base; equivalent to `setbase(10)'.

Manipulator: hex

Select hexadecimal base; equivalent to `setbase(16)'.

Manipulator: oct

Select octal base; equivalent to `setbase(8)'.

uppercase and nouppercase, modifies case of notation for numerical output

equivalent to setf(ios_base::uppercase) and unsetf(ios_base::uppercase)

case.cpp demonstrates the use of uppercase and nouppercase.

Example code

// modify uppercase flag
#include

using namespace std;

int main () {
___ double d = 123.45;
___ cout << showbase << hex;
___ cout << uppercase << 77 << endl;
___ cout << nouppercase << 77 << endl;
___
___ cout << scientific;
___ cout << d << endl;
___
___ return 0;
}

Output:

0X4D 0x4d

Manipulator: setfill (char padding)

Set the padding character, in the same way as ios::fill. Requires `#include '.

Manipulator: right

formats all strings on the right side of the output string.

Manipulator: left

formats all strings on the left side of the output string.

Mapping of stream manipulators to/from equivalent member functions

cout.width(n>
stream manipulator member function sample code
ws ws.cpp
flush cout.flush()
endl cout << "\n"; cout.flush();
ends cout << '\0';
setprecision(n) cout.precision(n) pi.cpp
setw(n) pi.cpp
oct cout.setf(ios_base::oct, ios_base::basefield); case.cpp
dec cout.setf(ios_base::dec, ios_base::basefield); case.cpp
hex cout.setf(ios_base::hex, ios_base::basefield); case.cpp
uppercase cout.setf(ios_base::uppercase); case.cpp
nouppercase cout.unsetf(ios_base::uppercase); case.cpp
setfill( c) cout.fill( c); width.cpp
right setf(ios_base::right, ios_base::adjustfield); width.cpp
left setf(ios_base::left, ios_base::adjustfield); width.cpp

Documentation about IOStreams

Rogue Wave's documentation about the C++ io library, which is mirrored here also.

Cplusplus.com's documentation about IO streams.

class ios_base

Rogue Wave has good documentation about the class ios_base . This class defines all of the ios_base format flags. Note that some of the stream manipulators are defined in iomanip. The

template class basic_ios

basic_ios is a template class which is specialized into ios, "typedef basic_ios< char> ios" and wios, "typedef basic_ios< wchar_t> wios;". The idea, is that this provides compatibility with ansi coding, as well as unicode, utf-8 etc.

template class basic_iostream

basic_iostream is a template clase which is a "virtual public" child of basic_ios. There are two specializations, "typedef basic_istream< char> istream;" and "typedef basic_istream< wchar_t> wistream;".

Format State Flags

The ios class defines an enumeration of format state flags that you can use to affect the formatting of data in USL I/O streams. The following list shows the formatting features and the format flags that control them:

Related Concepts This is a template.