MFC Programmer's SourceBook : Thinking in C++
Bruce Eckel's Thinking in C++, 2nd Ed Contents | Prev | Next

Formatting manipulators

As you can see from the previous example, calling the member functions can get a bit tedious. To make things easier to read and write, a set of manipulators is supplied to duplicate the actions provided by the member functions.

Manipulators with no arguments are provided in <iostream>. These include dec, oct, and hex , which perform the same action as, respectively, setf(ios::dec, ios::basefield) , setf(ios::oct, ios::basefield) , and setf(ios::hex, ios::basefield) , albeit more succinctly. <iostream>[47] also includes ws, endl, ends, and flush and the additional set shown here:

manipulator

effect

showbase

noshowbase

Indicate the numeric base (dec, oct, or hex) when printing an integral value. The format used can be read by the C++ compiler.

showpos

noshowpos

Show plus sign (+) for positive values

uppercase

nouppercase

Display uppercase A-F for hexadecimal values, and E for scientific values

showpoint

noshowpoint

Show decimal point and trailing zeros for floating-point values.

skipws

noskipws

Skip white space on input.

left

right

internal

Left-align, pad on right.

Right-align, pad on left.

Fill between leading sign or base indicator and value.

scientific

fixed

Use scientific notation

setprecision( ) or ios::precision( ) sets number of places after the decimal point.

Manipulators with arguments

If you are using manipulators with arguments, you must also include the header file < iomanip>. This contains code to solve the general problem of creating manipulators with arguments. In addition, it has six predefined manipulators:

manipulator

effect

setiosflags (fmtflags n)

Sets only the format flags specified by n. Setting remains in effect until the next change, like ios::setf( ).

resetiosflags(fmtflags n)

Clears only the format flags specified by n. Setting remains in effect until the next change, like ios::unsetf( ).

setbase(base n)

Changes base to n, where n is 10, 8, or 16. (Anything else results in 0.) If n is zero, output is base 10, but input uses the C conventions: 10 is 10, 010 is 8, and 0xf is 15. You might as well use dec, oct, and hex for output.

setfill(char n)

Changes the fill character to n, like ios::fill( ).

setprecision(int n)

Changes the precision to n, like ios::precision( ).

setw(int n)

Changes the field width to n, like ios::width( ).

If you’re using a lot of inserters, you can see how this can clean things up. As an example, here’s the previous program rewritten to use the manipulators. (The macro has been removed to make it easier to read.)

//: C18:Manips.cpp
// FORMAT.CPP using manipulators
#include <fstream>
#include <iomanip>
using namespace std;

int main() {
  ofstream trc("trace.out");
  int i = 47;
  float f = 2300114.414159;
  char* s = "Is there any more?";

  trc << setiosflags(
         ios::unitbuf /*| ios::stdio */ /// ?????
         | ios::showbase | ios::uppercase
         | ios::showpos);
  trc << i << endl; // Default to dec
  trc << hex << i << endl;
  trc << resetiosflags(ios::uppercase)
    << oct << i << endl;
  trc.setf(ios::left, ios::adjustfield);
  trc << resetiosflags(ios::showbase)
    << dec << setfill('0');
  trc << "fill char: " << trc.fill() << endl;
  trc << setw(10) << i << endl;
  trc.setf(ios::right, ios::adjustfield);
  trc << setw(10) << i << endl;
  trc.setf(ios::internal, ios::adjustfield);
  trc << setw(10) << i << endl;
  trc << i << endl; // Without setw(10)

  trc << resetiosflags(ios::showpos)
    << setiosflags(ios::showpoint)
    << "prec = " << trc.precision() << endl;
  trc.setf(ios::scientific, ios::floatfield);
  trc << f << endl;
  trc.setf(ios::fixed, ios::floatfield);
  trc << f << endl;
  trc.setf(0, ios::floatfield); // Automatic
  trc << f << endl;
  trc << setprecision(20);
  trc << "prec = " << trc.precision() << endl;
  trc << f << endl;
  trc.setf(ios::scientific, ios::floatfield);
  trc << f << endl;
  trc.setf(ios::fixed, ios::floatfield);
  trc << f << endl;
  trc.setf(0, ios::floatfield); // Automatic
  trc << f << endl;

  trc << setw(10) << s << endl;
  trc << setw(40) << s << endl;
  trc.setf(ios::left, ios::adjustfield);
  trc << setw(40) << s << endl;

  trc << resetiosflags(
         ios::showpoint | ios::unitbuf
         // | ios::stdio // ?????????
 );
} ///:~ 

You can see that a lot of the multiple statements have been condensed into a single chained insertion. Note the calls to setiosflags( ) and resetiosflags( ), where the flags have been bitwise-ORed together. This could also have been done with setf( ) and unsetf( ) in the previous example.


[47] These only appear in the revised library; you won’t find them in older implementations of iostreams.

Contents | Prev | Next


Go to CodeGuru.com
Contact: webmaster@codeguru.com
© Copyright 1997-1999 CodeGuru