JavaScript is required to for searching.
Skip Navigation Links
Exit Print View
Oracle Solaris Studio 12.3: C++ User's Guide     Oracle Solaris Studio 12.3 Information Library
search filter icon
search icon

Document Information


Part I C++ Compiler

1.  The C++ Compiler

2.  Using the C++ Compiler

3.  Using the C++ Compiler Options

Part II Writing C++ Programs

4.  Language Extensions

5.  Program Organization

6.  Creating and Using Templates

7.  Compiling Templates

8.  Exception Handling

9.  Improving Program Performance

10.  Building Multithreaded Programs

Part III Libraries

11.  Using Libraries

12.  Using the C++ Standard Library

13.  Using the Classic iostream Library

13.1 Predefined iostreams

13.2 Basic Structure of iostream Interaction

13.3 Using the Classic iostream Library

13.3.1 Output Using iostream Defining Your Own Insertion Operator Handling Output Errors Flushing Binary Output

13.3.2 Input Using iostream

13.3.3 Defining Your Own Extraction Operators

13.3.4 Using the char* Extractor

13.3.5 Reading Any Single Character

13.3.6 Binary Input

13.3.7 Peeking at Input

13.3.8 Extracting Whitespace

13.3.9 Handling Input Errors

13.3.10 Using iostreams With stdio

13.4 Creating iostreams

13.4.1 Dealing With Files Using Class fstream Open Mode Declaring an fstream Without Specifying a File Opening and Closing Files Opening a File Using a File Descriptor Repositioning Within a File

13.5 Assignment of iostreams

13.6 Format Control

13.7 Manipulators

13.7.1 Using Plain Manipulators

13.7.2 Parameterized Manipulators

13.8 strstream: iostreams for Arrays

13.9 stdiobuf: iostreams for stdio Files

13.10 Working Withstreambuf Streams

13.10.1 streambuf Pointer Types

13.10.2 Using streambuf Objects

13.11 iostream Man Pages

13.12 iostream Terminology

14.  Building Libraries

Part IV Appendixes

A.  C++ Compiler Options

B.  Pragmas



13.7 Manipulators

Manipulators are values that you can insert into or extract from iostreams to have special effects.

Parameterized manipulators are manipulators that take one or more parameters.

Because manipulators are ordinary identifiers and therefore use up possible names, iostream doesn’t define them for every possible function. A number of manipulators are discussed with member functions in other parts of this chapter.

The 13 predefined manipulators are described in the following table. This table assumes the following:

Table 13-2 iostream Predefined Manipulators

Predefined Manipulator
ostr << dec, istr >> dec
Makes the integer conversion base 10.
ostr << endl
Inserts a newline character (’\n’) and invokes ostream::flush().
ostr << ends
Inserts a null (0) character. Useful when dealing with strstream.
ostr << flush
Invokes ostream::flush().
ostr << hex, istr >> hex
Makes the integer conversion base 16.
ostr << oct, istr >> oct
Make the integer conversion base 8.
istr >> ws
Extracts whitespace characters (skips whitespace) until a non-whitespace character is found (which is left in istr).
ostr << setbase(n), istr >> setbase(n)
Sets the conversion base to n (0, 8, 10, 16 only).
ostr << setw(n), istr >> setw(n)
Invokes ios::width(n). Sets the field width to n.
ostr << resetiosflags(i), istr>> resetiosflags(i)
Clears the flags bitvector according to the bits set in i.
ostr << setiosflags(i), istr >> setiosflags(i)
Sets the flags bitvector according to the bits set in i.
ostr << setfill(c), istr >> setfill(c)
Sets the fill character (for padding a field) to c.
ostr << setprecision(n), istr >> setprecision(n)
Sets the floating-point precision to n digits.

To use predefined manipulators, you must include the file iomanip.h in your program.

You can define your own manipulators. The two basic types of manipulators are:

13.7.1 Using Plain Manipulators

A plain manipulator is a function that performs the following actions:

The shift operators taking a pointer to such a function are predefined for iostreams so the function can be put in a sequence of input or output operators. The shift operator calls the function rather than trying to read or write a value. The following example shows a tab manipulator that inserts a tab in an ostream is:

ostream& tab(ostream& os) {
             return os <<’\t’;
cout << x << tab << y;

This example is an elaborate way to achieve the following code:

const char tab = ’\t’;
cout << x << tab << y;

The following code is another example, which cannot be accomplished with a simple constant. Suppose you want to turn whitespace skipping on and off for an input stream. You can use separate calls to ios::setf and ios::unsetf to turn the skipws flag on and off, or you could define two manipulators.

#include <iostream.h>
#include <iomanip.h>
istream& skipon(istream &is) {
       is.setf(ios::skipws, ios::skipws);
       return is;
istream& skipoff(istream& is) {
       return is;
int main ()
      int x,y;
      cin >> skipon >> x >> skipoff >> y;
      return 1;

13.7.2 Parameterized Manipulators

One of the parameterized manipulators that is included in iomanip.h is setfill. setfill sets the character that is used to fill out field widths. This manipulator is implemented as shown in the following example:


//the private manipulator
static ios& sfill(ios& i, int f) {
         return i;
//the public applicator
smanip_int setfill(int f) {
       return smanip_int(sfill, f);

A parameterized manipulator is implemented in two parts:

Several classes are defined in the header file iomanip.h. Each class holds the address of a manipulator function and the value of one parameter. The iomanip classes are described in the manip(3CC4) man page. The previous example uses the smanip_int class, which works with an ios. Because it works with an ios, it also works with an istream and an ostream. The previous example also uses a second parameter of type int.

The applicator creates and returns a class object. In the previous code example the class object is an smanip_int, and it contains the manipulator and the int argument to the applicator. The iomanip.h header file defines the shift operators for this class. When the applicator function setfill appears in a sequence of input or output operations, the applicator function is called, and it returns a class. The shift operator acts on the class to call the manipulator function with its parameter value, which is stored in the class.

In the following example, the manipulator print_hex performs the following actions:

The class omanip_long is used because this code example is for output only. It operates on a long rather than an int:

#include <iostream.h>
#include <iomanip.h>
static ostream& xfield(ostream& os, long v) {
        long save = os.setf(ios::hex, ios::basefield);
        os << v;
        os.setf(save, ios::basefield);
        return os;
omanip_long print_hex(long v) {
       return omanip_long(xfield, v);