C++ Formatted IO

The iomanip.h and iostream.h header files are used to perform the formatted IO operations in C++.

The C++ programming language provides the several built-in functions to display the output in formatted form. These built-in functions are available in the header file iomanip.h and ios class of header file iostream.h.

In C++, there are two ways to perform the formatted IO operations.

  • Using the member functions of ios class.
  • Using the special functions called manipulators defined in iomanip.h.

Formatted IO using ios class memebers

The ios class contains several member functions that are used to perform formmated IO operations.

The ios class also contains few format flags used to format the output. It has format flags like showpos, showbase, oct, hex, etc. The format flags are used by the function setf( ).

The following table provides the details of the functions of ios class used to perform formatted IO in C++.

Function Description
width(int) Used to set the width in number of character spaces for the immediate output data.
fill(char) Used to fill the blank spaces in output with given character.
precision(int) Used to set the number of the decimal point to a float value.
setf(format flags) Used to set various flags for formatting output like showbase, showpos, oct, hex, etc.
unsetf(format flags) Used to clear the format flag setting.

All the above functions are called using the built-in object cout.

Lets look at the following code to illustrate the formatted IO operations using member functions of ios class.

Example - Code to illustrate the formatted IO using ios class
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    cout << "Example for formatted IO" << endl;

    cout << "Default: " << endl;
    cout << 123 << endl;

    cout << "width(5): " << endl;
    cout.width(5);
    cout << 123 << endl;

    cout << "width(5) and fill('*'): " << endl;
    cout.width(5);
    cout.fill('*');
    cout << 123 << endl;

    cout.precision(5);
    cout << "precision(5) ---> " << 123.4567890 << endl;
    cout << "precision(5) ---> " << 9.876543210 << endl;

    cout << "setf(showpos): " << endl;
    cout.setf(ios::showpos);
    cout << 123 << endl;

    cout << "unsetf(showpos): " << endl;
    cout.unsetf(ios::showpos);
    cout << 123 << endl;

    return 0;
}
Output
formatted io using ios class example program in c++

Formatted IO using manipulators

The iomanip.h header file contains several special functions that are used to perform formmated IO operations.

The following table provides the details of the special manipulator functions used to perform formatted IO in C++.

Function Description
setw(int) Used to set the width in number of characters for the immediate output data.
setfill(char) Used to fill the blank spaces in output with given character.
setprecision(int) Used to set the number of digits of precision.
setbase(int) Used to set the number base.
setiosflags(format flags) Used to set the format flag.
resetiosflags(format flags) Used to clear the format flag.

The iomanip.h also contains the following format flags using in formatted IO in C++.

Flag Description
endl Used to move the cursor position to a newline.
ends Used to print a blank space (null character).
dec Used to set the decimal flag.
oct Used to set the octal flag.
hex Used to set the hexadecimal flag.
left Used to set the left alignment flag.
right Used to set the right alignment flag.
showbase Used to set the showbase flag.
noshowbase Used to set the noshowbase flag.
showpos Used to set the showpos flag.
noshowpos Used to set the noshowpos flag.
showpoit Used to set the showpoit flag.
noshowpoint Used to set the noshowpoint flag.

Lets look at the following code to illustrate the formatted IO operations using manipulators.

Example - Code to illustrate the formatted IO using manipulators
#include <iostream>
#include <fstream>

using namespace std;

void line() {
    cout << "-------------------------------" << endl;
}

int main()
{
    cout << "Example for formatted IO" << endl;
    line();
    cout << "setw(10): " << endl;
    cout << setw(10) << 99 << endl;
    line();
    cout << "setw(10) and setfill('*'): " << endl;
    cout << setw(10) << setfill('*') << 99 << endl;
    line();
    cout << "setprecision(5): " << endl;
    cout << setprecision(5) << 123.4567890 << endl;
    line();
    cout << "showpos: " << endl;
    cout << showpos << 999 << endl;
    line();
    cout << "hex: " << endl;
    cout << hex << 100 << endl;
    line();
    cout << "hex and showbase: " << endl;
    cout << showbase << hex << 100 << endl;
    line();

    return 0;
}
Output
formatted io using manipulators example program in c++
  • To use the io manipulators the iomanip.h header file must be included.