Formatted I/O in C++

24 Oct, 2019
C++ helps you to format the I/O operations like determining the number of digits to be displayed after the decimal point, specifying number base etc.
Example:
If we want to add + sign as the prefix of out output, we can use the formatting to do so:stream.setf(ios::showpos)  
If input=100, output will be +100 
stream.setf(ios::showpos)  
If input=100, output will be +100 
If we want to add trailing zeros in out output to be shown when needed using the formatting:stream.setf(ios::showpoint) 
If input=100.0, output will be 100.000
stream.setf(ios::showpoint) 
If input=100.0, output will be 100.000
Note: Here, stream is referred to the streams defined in c++ like cin, cout, cerr, clog.
There are two ways to do so:
Using the ios class or various ios member functions.Using manipulators(special functions)
Using the ios class or various ios member functions.
Using manipulators(special functions)
Formatting using the ios members:The stream has the format flags that control the way of formatting it means Using this setf function, we can set the flags, which allow us to display a value in a particular format. The ios class declares a bitmask enumeration called fmtflags in which the values(showbase, showpoint, oct, hex etc) are defined. These values are used to set or clear the format flags.Few standard ios class functions are:width(): The width method is used to set the required field width. The output will be displayed in the given widthprecision(): The precision method is used to set the number of the decimal point to a float valuefill(): The fill method is used to set a character to fill in the blank space of a fieldsetf(): The setf method is used to set various flags for formatting outputunsetf(): The unsetf method is used To remove the flag settingWorking:#include<bits/stdc++.h>using namespace std;  // The width() function defines width // of the next value to be displayed // in the output at the console.void IOS_width(){    cout << "--------------------------\n";    cout << "Implementing ios::width\n\n";          char c = 'A';           // Adjusting width will be 5.    cout.width(5);           cout << c <<"\n";                  int temp = 10;           // Width of the next value to be    // displayed in the output will     // not be adjusted to 5 columns.    cout<<temp;    cout << "\n--------------------------\n";}  void IOS_precision(){    cout << "\n--------------------------\n";    cout << "Implementing ios::precision\n\n";    cout << "Implementing ios::width";    cout.setf(ios::fixed, ios::floatfield);    cout.precision(2);    cout<<3.1422;    cout << "\n--------------------------\n";}  // The fill() function fills the unused // white spaces in a value (to be printed// at the console), with a character of choice.void IOS_fill(){    cout << "\n--------------------------\n";    cout << "Implementing ios::fill\n\n";    char ch = 'a';          // Calling the fill function to fill     // the white spaces in a value with a    // character our of choice.    cout.fill('*');          cout.width(10);    cout<<ch <<"\n";                int i = 1;          // Once you call the fill() function,     // you don't have to call it again to     // fill the white space in a value with    // the same character.    cout.width(5);    cout<<i;    cout << "\n--------------------------\n";}  void IOS_setf(){    cout << "\n--------------------------\n";    cout << "Implementing ios::setf\n\n";    int val1=100,val2=200;    cout.setf(ios::showpos);    cout<<val1<<" "<<val2;    cout << "\n--------------------------\n";}  void IOS_unsetf(){    cout << "\n--------------------------\n";    cout << "Implementing ios::unsetf\n\n";    cout.setf(ios::showpos|ios::showpoint);    // Clear the showflag flag without     // affecting the showpoint flag    cout.unsetf(ios::showpos);     cout<<200.0;     cout << "\n--------------------------\n";}  // Driver Methodint main(){    IOS_width();    IOS_precision;    IOS_fill();    IOS_setf();    IOS_unsetf();    return 0;} Output:--------------------------
Implementing ios::width

    A
10
--------------------------

--------------------------
Implementing ios::fill

*********a
****1
--------------------------

--------------------------
Implementing ios::setf

+100 +200
--------------------------

--------------------------
Implementing ios::unsetf

200.000
--------------------------
Formatting using ManipulatorsThe second way you can alter the format parameters of a stream is through the use of special functions called manipulators that can be included in an I/O expression.The standard manipulators are shown below:boolalpha: The boolalpha manipulator of stream manipulators in C++ is used to turn on bool alpha flagdec: The dec manipulator of stream manipulators in C++ is used to turn on the dec flagendl: The endl manipulator of stream manipulators in C++ is used to Output a newline character.and: The and manipulator of stream manipulators in C++ is used to Flush the streamends: The ends manipulator of stream manipulators in C++ is used to Output a nullfixed: The fixed manipulator of stream manipulators in C++ is used to Turns on the fixed flagflush: The flush manipulator of stream manipulators in C++ is used to Flush a streamhex: The hex manipulator of stream manipulators in C++ is used to Turns on hex flaginternal: The internal manipulator of stream manipulators in C++ is used to Turns on internal flagleft: The left manipulator of stream manipulators in C++ is used to Turns on the left flagnoboolalpha: The noboolalpha manipulator of stream manipulators in C++ is used to Turns off bool alpha flagnoshowbase: The noshowbase manipulator of stream manipulators in C++ is used to Turns off showcase flagnoshowpoint: The noshowpoint manipulator of stream manipulators in C++ is used to Turns off show point flagnoshowpos: The noshowpos manipulator of stream manipulators in C++ is used to Turns off showpos flagnoskipws: The noskipws manipulator of stream manipulators in C++ is used to Turns off skipws flagnounitbuf: The nounitbuf manipulator of stream manipulators in C++ is used to Turns off the unit buff flagnouppercase: The nouppercase manipulator of stream manipulators in C++ is used to Turns off the uppercase flagoct: The oct manipulator of stream manipulators in C++ is used to Turns on oct flagresetiosflags(fmtflags f): The resetiosflags manipulator of stream manipulators in C++ is used to Turns off the flag specified in fright: The right manipulator of stream manipulators in C++ is used to Turns on the right flagscientific: The scientific manipulator of stream manipulators in C++ is used to Turns on scientific flagsetbase(int base): The setbase manipulator of stream manipulators in C++ is used to Set the number base to basesetfill(int ch): The setfill manipulator of stream manipulators in C++ is used to Set the fill character to chsetiosflags(fmtflags f): The setiosflags manipulator of stream manipulators in C++ is used to Turns on the flag specified in fsetprecision(int p): The setprecision manipulator of stream manipulators in C++ is used to Set the number of digits of precisionsetw(int w): The setw manipulator of stream manipulators in C++ is used to Set the field width to wshowbase: The showbase manipulator of stream manipulators in C++ is used to Turns on showbase flagshowpoint: The showpoint manipulator of stream manipulators in C++ is used to Turns on show point flagshowpos: The showpos manipulator of stream manipulators in C++ is used to Turns on showpos flagskipws: The skipws manipulator of stream manipulators in C++ is used to Turns on skipws flagunitbuf: The unitbuf manipulator of stream manipulators in C++ is used to turn on unitbuf flaguppercase: The uppercase manipulator of stream manipulators in C++ is used to turn on the uppercase flagws: The ws manipulator of stream manipulators in C++ is used to skip leading white spaceTo access manipulators that take parameters (such as setw( )), you must include “iomanip” header file in your program.Example:#include <iomanip>#include <iostream>using namespace std;  void Example(){    // performs ssame as setf( )    cout << setiosflags(ios::showpos);    cout << 123<<"\n";          // hexadecimal base (i.e., radix 16)    cout << hex << 100 << endl;           // Set the field width    cout << setfill('*') << setw(10) << 2343.0; }  int main(){    Example();    return 0;}Output:+123
64
*****+2343
The manipulator setiosflags( ) .
Formatting using the ios members:The stream has the format flags that control the way of formatting it means Using this setf function, we can set the flags, which allow us to display a value in a particular format. The ios class declares a bitmask enumeration called fmtflags in which the values(showbase, showpoint, oct, hex etc) are defined. These values are used to set or clear the format flags.Few standard ios class functions are:width(): The width method is used to set the required field width. The output will be displayed in the given widthprecision(): The precision method is used to set the number of the decimal point to a float valuefill(): The fill method is used to set a character to fill in the blank space of a fieldsetf(): The setf method is used to set various flags for formatting outputunsetf(): The unsetf method is used To remove the flag settingWorking:#include<bits/stdc++.h>using namespace std;  // The width() function defines width // of the next value to be displayed // in the output at the console.void IOS_width(){    cout << "--------------------------\n";    cout << "Implementing ios::width\n\n";          char c = 'A';           // Adjusting width will be 5.    cout.width(5);           cout << c <<"\n";                  int temp = 10;           // Width of the next value to be    // displayed in the output will     // not be adjusted to 5 columns.    cout<<temp;    cout << "\n--------------------------\n";}  void IOS_precision(){    cout << "\n--------------------------\n";    cout << "Implementing ios::precision\n\n";    cout << "Implementing ios::width";    cout.setf(ios::fixed, ios::floatfield);    cout.precision(2);    cout<<3.1422;    cout << "\n--------------------------\n";}  // The fill() function fills the unused // white spaces in a value (to be printed// at the console), with a character of choice.void IOS_fill(){    cout << "\n--------------------------\n";    cout << "Implementing ios::fill\n\n";    char ch = 'a';          // Calling the fill function to fill     // the white spaces in a value with a    // character our of choice.    cout.fill('*');          cout.width(10);    cout<<ch <<"\n";                int i = 1;          // Once you call the fill() function,     // you don't have to call it again to     // fill the white space in a value with    // the same character.    cout.width(5);    cout<<i;    cout << "\n--------------------------\n";}  void IOS_setf(){    cout << "\n--------------------------\n";    cout << "Implementing ios::setf\n\n";    int val1=100,val2=200;    cout.setf(ios::showpos);    cout<<val1<<" "<<val2;    cout << "\n--------------------------\n";}  void IOS_unsetf(){    cout << "\n--------------------------\n";    cout << "Implementing ios::unsetf\n\n";    cout.setf(ios::showpos|ios::showpoint);    // Clear the showflag flag without     // affecting the showpoint flag    cout.unsetf(ios::showpos);     cout<<200.0;     cout << "\n--------------------------\n";}  // Driver Methodint main(){    IOS_width();    IOS_precision;    IOS_fill();    IOS_setf();    IOS_unsetf();    return 0;} Output:--------------------------
Implementing ios::width

    A
10
--------------------------

--------------------------
Implementing ios::fill

*********a
****1
--------------------------

--------------------------
Implementing ios::setf

+100 +200
--------------------------

--------------------------
Implementing ios::unsetf

200.000
--------------------------

The stream has the format flags that control the way of formatting it means Using this setf function, we can set the flags, which allow us to display a value in a particular format. The ios class declares a bitmask enumeration called fmtflags in which the values(showbase, showpoint, oct, hex etc) are defined. These values are used to set or clear the format flags.
Few standard ios class functions are:
width(): The width method is used to set the required field width. The output will be displayed in the given widthprecision(): The precision method is used to set the number of the decimal point to a float valuefill(): The fill method is used to set a character to fill in the blank space of a fieldsetf(): The setf method is used to set various flags for formatting outputunsetf(): The unsetf method is used To remove the flag setting
width(): The width method is used to set the required field width. The output will be displayed in the given width
precision(): The precision method is used to set the number of the decimal point to a float value
fill(): The fill method is used to set a character to fill in the blank space of a field
setf(): The setf method is used to set various flags for formatting output
unsetf(): The unsetf method is used To remove the flag setting
Working:
#include<bits/stdc++.h>using namespace std;  // The width() function defines width // of the next value to be displayed // in the output at the console.void IOS_width(){    cout << "--------------------------\n";    cout << "Implementing ios::width\n\n";          char c = 'A';           // Adjusting width will be 5.    cout.width(5);           cout << c <<"\n";                  int temp = 10;           // Width of the next value to be    // displayed in the output will     // not be adjusted to 5 columns.    cout<<temp;    cout << "\n--------------------------\n";}  void IOS_precision(){    cout << "\n--------------------------\n";    cout << "Implementing ios::precision\n\n";    cout << "Implementing ios::width";    cout.setf(ios::fixed, ios::floatfield);    cout.precision(2);    cout<<3.1422;    cout << "\n--------------------------\n";}  // The fill() function fills the unused // white spaces in a value (to be printed// at the console), with a character of choice.void IOS_fill(){    cout << "\n--------------------------\n";    cout << "Implementing ios::fill\n\n";    char ch = 'a';          // Calling the fill function to fill     // the white spaces in a value with a    // character our of choice.    cout.fill('*');          cout.width(10);    cout<<ch <<"\n";                int i = 1;          // Once you call the fill() function,     // you don't have to call it again to     // fill the white space in a value with    // the same character.    cout.width(5);    cout<<i;    cout << "\n--------------------------\n";}  void IOS_setf(){    cout << "\n--------------------------\n";    cout << "Implementing ios::setf\n\n";    int val1=100,val2=200;    cout.setf(ios::showpos);    cout<<val1<<" "<<val2;    cout << "\n--------------------------\n";}  void IOS_unsetf(){    cout << "\n--------------------------\n";    cout << "Implementing ios::unsetf\n\n";    cout.setf(ios::showpos|ios::showpoint);    // Clear the showflag flag without     // affecting the showpoint flag    cout.unsetf(ios::showpos);     cout<<200.0;     cout << "\n--------------------------\n";}  // Driver Methodint main(){    IOS_width();    IOS_precision;    IOS_fill();    IOS_setf();    IOS_unsetf();    return 0;} 
--------------------------
Implementing ios::width

    A
10
--------------------------

--------------------------
Implementing ios::fill

*********a
****1
--------------------------

--------------------------
Implementing ios::setf

+100 +200
--------------------------

--------------------------
Implementing ios::unsetf

200.000
--------------------------

Formatting using ManipulatorsThe second way you can alter the format parameters of a stream is through the use of special functions called manipulators that can be included in an I/O expression.The standard manipulators are shown below:boolalpha: The boolalpha manipulator of stream manipulators in C++ is used to turn on bool alpha flagdec: The dec manipulator of stream manipulators in C++ is used to turn on the dec flagendl: The endl manipulator of stream manipulators in C++ is used to Output a newline character.and: The and manipulator of stream manipulators in C++ is used to Flush the streamends: The ends manipulator of stream manipulators in C++ is used to Output a nullfixed: The fixed manipulator of stream manipulators in C++ is used to Turns on the fixed flagflush: The flush manipulator of stream manipulators in C++ is used to Flush a streamhex: The hex manipulator of stream manipulators in C++ is used to Turns on hex flaginternal: The internal manipulator of stream manipulators in C++ is used to Turns on internal flagleft: The left manipulator of stream manipulators in C++ is used to Turns on the left flagnoboolalpha: The noboolalpha manipulator of stream manipulators in C++ is used to Turns off bool alpha flagnoshowbase: The noshowbase manipulator of stream manipulators in C++ is used to Turns off showcase flagnoshowpoint: The noshowpoint manipulator of stream manipulators in C++ is used to Turns off show point flagnoshowpos: The noshowpos manipulator of stream manipulators in C++ is used to Turns off showpos flagnoskipws: The noskipws manipulator of stream manipulators in C++ is used to Turns off skipws flagnounitbuf: The nounitbuf manipulator of stream manipulators in C++ is used to Turns off the unit buff flagnouppercase: The nouppercase manipulator of stream manipulators in C++ is used to Turns off the uppercase flagoct: The oct manipulator of stream manipulators in C++ is used to Turns on oct flagresetiosflags(fmtflags f): The resetiosflags manipulator of stream manipulators in C++ is used to Turns off the flag specified in fright: The right manipulator of stream manipulators in C++ is used to Turns on the right flagscientific: The scientific manipulator of stream manipulators in C++ is used to Turns on scientific flagsetbase(int base): The setbase manipulator of stream manipulators in C++ is used to Set the number base to basesetfill(int ch): The setfill manipulator of stream manipulators in C++ is used to Set the fill character to chsetiosflags(fmtflags f): The setiosflags manipulator of stream manipulators in C++ is used to Turns on the flag specified in fsetprecision(int p): The setprecision manipulator of stream manipulators in C++ is used to Set the number of digits of precisionsetw(int w): The setw manipulator of stream manipulators in C++ is used to Set the field width to wshowbase: The showbase manipulator of stream manipulators in C++ is used to Turns on showbase flagshowpoint: The showpoint manipulator of stream manipulators in C++ is used to Turns on show point flagshowpos: The showpos manipulator of stream manipulators in C++ is used to Turns on showpos flagskipws: The skipws manipulator of stream manipulators in C++ is used to Turns on skipws flagunitbuf: The unitbuf manipulator of stream manipulators in C++ is used to turn on unitbuf flaguppercase: The uppercase manipulator of stream manipulators in C++ is used to turn on the uppercase flagws: The ws manipulator of stream manipulators in C++ is used to skip leading white spaceTo access manipulators that take parameters (such as setw( )), you must include “iomanip” header file in your program.Example:#include <iomanip>#include <iostream>using namespace std;  void Example(){    // performs ssame as setf( )    cout << setiosflags(ios::showpos);    cout << 123<<"\n";          // hexadecimal base (i.e., radix 16)    cout << hex << 100 << endl;           // Set the field width    cout << setfill('*') << setw(10) << 2343.0; }  int main(){    Example();    return 0;}Output:+123
64
*****+2343
The manipulator setiosflags( ) .
boolalpha: The boolalpha manipulator of stream manipulators in C++ is used to turn on bool alpha flagdec: The dec manipulator of stream manipulators in C++ is used to turn on the dec flagendl: The endl manipulator of stream manipulators in C++ is used to Output a newline character.and: The and manipulator of stream manipulators in C++ is used to Flush the streamends: The ends manipulator of stream manipulators in C++ is used to Output a nullfixed: The fixed manipulator of stream manipulators in C++ is used to Turns on the fixed flagflush: The flush manipulator of stream manipulators in C++ is used to Flush a streamhex: The hex manipulator of stream manipulators in C++ is used to Turns on hex flaginternal: The internal manipulator of stream manipulators in C++ is used to Turns on internal flagleft: The left manipulator of stream manipulators in C++ is used to Turns on the left flagnoboolalpha: The noboolalpha manipulator of stream manipulators in C++ is used to Turns off bool alpha flagnoshowbase: The noshowbase manipulator of stream manipulators in C++ is used to Turns off showcase flagnoshowpoint: The noshowpoint manipulator of stream manipulators in C++ is used to Turns off show point flagnoshowpos: The noshowpos manipulator of stream manipulators in C++ is used to Turns off showpos flagnoskipws: The noskipws manipulator of stream manipulators in C++ is used to Turns off skipws flagnounitbuf: The nounitbuf manipulator of stream manipulators in C++ is used to Turns off the unit buff flagnouppercase: The nouppercase manipulator of stream manipulators in C++ is used to Turns off the uppercase flagoct: The oct manipulator of stream manipulators in C++ is used to Turns on oct flagresetiosflags(fmtflags f): The resetiosflags manipulator of stream manipulators in C++ is used to Turns off the flag specified in fright: The right manipulator of stream manipulators in C++ is used to Turns on the right flagscientific: The scientific manipulator of stream manipulators in C++ is used to Turns on scientific flagsetbase(int base): The setbase manipulator of stream manipulators in C++ is used to Set the number base to basesetfill(int ch): The setfill manipulator of stream manipulators in C++ is used to Set the fill character to chsetiosflags(fmtflags f): The setiosflags manipulator of stream manipulators in C++ is used to Turns on the flag specified in fsetprecision(int p): The setprecision manipulator of stream manipulators in C++ is used to Set the number of digits of precisionsetw(int w): The setw manipulator of stream manipulators in C++ is used to Set the field width to wshowbase: The showbase manipulator of stream manipulators in C++ is used to Turns on showbase flagshowpoint: The showpoint manipulator of stream manipulators in C++ is used to Turns on show point flagshowpos: The showpos manipulator of stream manipulators in C++ is used to Turns on showpos flagskipws: The skipws manipulator of stream manipulators in C++ is used to Turns on skipws flagunitbuf: The unitbuf manipulator of stream manipulators in C++ is used to turn on unitbuf flaguppercase: The uppercase manipulator of stream manipulators in C++ is used to turn on the uppercase flagws: The ws manipulator of stream manipulators in C++ is used to skip leading white space
boolalpha: The boolalpha manipulator of stream manipulators in C++ is used to turn on bool alpha flag
dec: The dec manipulator of stream manipulators in C++ is used to turn on the dec flag
endl: The endl manipulator of stream manipulators in C++ is used to Output a newline character.
and: The and manipulator of stream manipulators in C++ is used to Flush the stream
ends: The ends manipulator of stream manipulators in C++ is used to Output a null
fixed: The fixed manipulator of stream manipulators in C++ is used to Turns on the fixed flag
flush: The flush manipulator of stream manipulators in C++ is used to Flush a stream
hex: The hex manipulator of stream manipulators in C++ is used to Turns on hex flag
internal: The internal manipulator of stream manipulators in C++ is used to Turns on internal flag
left: The left manipulator of stream manipulators in C++ is used to Turns on the left flag
noboolalpha: The noboolalpha manipulator of stream manipulators in C++ is used to Turns off bool alpha flag
noshowbase: The noshowbase manipulator of stream manipulators in C++ is used to Turns off showcase flag
noshowpoint: The noshowpoint manipulator of stream manipulators in C++ is used to Turns off show point flag
noshowpos: The noshowpos manipulator of stream manipulators in C++ is used to Turns off showpos flag
noskipws: The noskipws manipulator of stream manipulators in C++ is used to Turns off skipws flag
nounitbuf: The nounitbuf manipulator of stream manipulators in C++ is used to Turns off the unit buff flag
nouppercase: The nouppercase manipulator of stream manipulators in C++ is used to Turns off the uppercase flag
oct: The oct manipulator of stream manipulators in C++ is used to Turns on oct flag
resetiosflags(fmtflags f): The resetiosflags manipulator of stream manipulators in C++ is used to Turns off the flag specified in f
right: The right manipulator of stream manipulators in C++ is used to Turns on the right flag
scientific: The scientific manipulator of stream manipulators in C++ is used to Turns on scientific flag
setbase(int base): The setbase manipulator of stream manipulators in C++ is used to Set the number base to base
setfill(int ch): The setfill manipulator of stream manipulators in C++ is used to Set the fill character to ch
setiosflags(fmtflags f): The setiosflags manipulator of stream manipulators in C++ is used to Turns on the flag specified in f
setprecision(int p): The setprecision manipulator of stream manipulators in C++ is used to Set the number of digits of precision
setw(int w): The setw manipulator of stream manipulators in C++ is used to Set the field width to w
showbase: The showbase manipulator of stream manipulators in C++ is used to Turns on showbase flag
showpoint: The showpoint manipulator of stream manipulators in C++ is used to Turns on show point flag
showpos: The showpos manipulator of stream manipulators in C++ is used to Turns on showpos flag
skipws: The skipws manipulator of stream manipulators in C++ is used to Turns on skipws flag
unitbuf: The unitbuf manipulator of stream manipulators in C++ is used to turn on unitbuf flag
uppercase: The uppercase manipulator of stream manipulators in C++ is used to turn on the uppercase flag
ws: The ws manipulator of stream manipulators in C++ is used to skip leading white space
To access manipulators that take parameters (such as setw( )), you must include “iomanip” header file in your program.
Example:
#include <iomanip>#include <iostream>using namespace std;  void Example(){    // performs ssame as setf( )    cout << setiosflags(ios::showpos);    cout << 123<<"\n";          // hexadecimal base (i.e., radix 16)    cout << hex << 100 << endl;           // Set the field width    cout << setfill('*') << setw(10) << 2343.0; }  int main(){    Example();    return 0;}
+123
64
*****+2343

The manipulator setiosflags( ) .
Akanksha_Rai
cpp-input-output
C++ Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.