Deitel & Associates, Inc. Logo

Back to www.deitel.com
digg.png delicious.png blinkit.png furl.png
C++ How to Program, 5/e

ISBN:
0-13-185757-6
© 2005
pages: 1500
Buy the Book!
Amazon logo
InformIT logo

This tutorial introduces ANSI/ISO C++'s stream formatting capabilties, including stream manipulators, justification, padding, integer formats, floating-point number formats, uppercase/lowercase control, formatting booleans as strings, and setting and restoring stream format states. This tutorial is intented for students and developers who are familiar with basic C++ input and output techniques using cin and cout. The techniques shown here can also be applied to file-based streams or other streams that extend the standard C++ stream class hierarchy.

Download the code examples for this tutorial.

[Note: This tutorial is an excerpt (Section 15.7) of Chapter 15, Stream Input/Output, from our textbook C++ How to Program, 5/e. These tutorials may refer to other chapters or sections of the book that are not included here. Permission Information: Deitel, Harvey M. and Paul J., C++ HOW TO PROGRAM, ©2005, pp.787-797. Electronically reproduced by permission of Pearson Education, Inc., Upper Saddle River, New Jersey.]

15.7 Stream Format States and Stream Manipulators

Various stream manipulators can be used to specify the kinds of formatting to be performed during stream-I/O operations. Stream manipulators control the output’s format settings. Figure 15.12 lists each stream manipulator that controls a given stream’s format state. All these manipulators belong to class ios_base. We show examples of most of these stream manipulators in the next several sections.

Fig. 15.12 Format state stream manipulators from <iostream>.

Stream Manipulator Description
skipws Skip white-space characters on an input stream. This setting is reset with stream manipulator noskipws.
left Left justify output in a field. Padding characters appear to the right if necessary.
right Right justify output in a field. Padding characters appear to the left if necessary.
internal Indicate that a number’s sign should be left justified in a field and a number’s magnitude should be right justified in that same field (i.e., padding characters appear between the sign and the number).
dec Specify that integers should be treated as decimal (base 10) values.
oct Specify that integers should be treated as octal (base 8) values.
hex Specify that integers should be treated as hexadecimal (base 16) values.
showbase Specify that the base of a number is to be output ahead of the number (a leading 0 for octals; a leading 0x or 0X for hexadecimals). This setting is reset with stream manipulator noshowbase.
showpoint Specify that floating-point numbers should be output with a decimal point. This is used normally with fixed to guarantee a certain number of digits to the right of the decimal point, even if they are zeros. This setting is reset with stream manipulator noshowpoint.
uppercase Specify that uppercase letters (i.e., X and A through F) should be used in a hexadecimal integer and that uppercase E should be used when representing a floating-point value in scientific notation. This setting is reset with stream manipulator nouppercase.
showpos Specify that positive numbers should be preceded by a plus sign (+). This setting is reset with stream manipulator noshowpos.
scientific Specify output of a floating-point value in scientific notation.
fixed Specify output of a floating-point value in fixed-point notation with a specific number of digits to the right of the decimal point.
 

Other sections in this tutorial:
Introduction to Stream Format States and Stream Manipulators (You are here).
15.7.1 Trailing Zeros and Decimal Points (showpoint)
15.7.2 Justification (left, right and internal)
15.7.3 Padding (fill, setfill)
15.7.4 Integral Stream Base (dec, oct, hex, showbase)
15.7.5 Floating-Point Numbers; Scientific and Fixed Notation (scientific, fixed)
15.7.6 Uppercase/Lowercase Control (uppercase)
15.7.7 Specifying Boolean Format (boolalpha)
15.7.8 Setting and Resetting the Format State via Member Function flags

Tutorial Index