Sams Teach Yourself C++ in 21 Days

(singke) #1
Working with Streams 623

17


For example,
cout << “\aAn error occured\t”
rings the bell, prints an error message, and moves to the next tab stop. Manipulators are
used with the cout operator. Those manipulators that take arguments require that you
include iomanip in your file.
The following is a list of manipulators that do notrequire iomanip:
flush—Flushes the output buffer
endl—Inserts newline and flushes the output buffer
oct—Sets output base to octal
dec—Sets output base to decimal
hex—Sets output base to hexadecimal

The following is a list of manipulators that do require iomanip:
setbase(base)—Sets output base (0 = decimal, 8 = octal, 10 = decimal, 16 = hex)
setw(width)—Sets minimum output field width
setfill(ch)—Fills character to be used when width is defined
setprecision(p)—Sets precision for floating-point numbers
setiosflags(f)—Sets one or more iosflags
resetiosflags(f)—Resets one or more iosflags

For example,
cout << setw(12) << setfill(‘#’) << hex << x << endl;
sets the field width to 12, sets the fill character to ‘#’, specifies hex output, prints the
value of ‘x’, puts a newline in the buffer, and flushes the buffer. All the manipulators
except flush, endl, and setwremain in effect until changed or until the end of the
program. setwreturns to the default after the current cout.
A number of flags can be used with the setiosflags and resetiosflags manipulators.
These were listed in Table 17.1 earlier.
Additional information can be obtained from file iosand from your compiler’s
documentation.

File Input and Output ..........................................................................................


Streams provide a uniform way of dealing with data coming from the keyboard or the
hard disk and going out to the console screen or hard disk. In either case, you can use the
insertion and extraction operators or the other related functions and manipulators. To
Free download pdf