Last Updated:

Formatted I/O in C++

In this tutorial, we'll go back to the previously used cin and cout operators to cover them in the organization of formatted I/O. To control I/O, C++
uses:

  • Formatted I/O flags
  • formatting manipulators.

Flags allow you to set I/O parameters that will apply to all subsequent I/O statements until they are canceled. The manipulators are inserted into the cin(cout) statements and set the parameters of the current I/O statement.

Using flags

Flags allow you to enable or disable one of the display options. To set the output flag, use the following C++ construct:

cout.setf(ios::flag)

To remove the flag design:

cout.unsetf(ios::flag)

Here, flag is the name of the specific flag.

If you need to set multiple flags during output, you can use the arithmetic operation "or" (|). In this case, the construction of the C++ language would be as follows:

cout.setf(ios::flag1 | ios::flag2 | ios::flag3)

In this case, flag1, flag2, flag3 are the names of the output flags to be set. The following table lists some of the formatted output flags with examples of how to use them.

FlagDescriptionExample of useResult
rightRight alignment 

 

int r=25;
coutsetf(ios::right);
coutwidth(15);
cout<<«r=»<<r<<endl
;

 

r=-25
leftLeft alignment (default) 

 

double r=25.45;
coutsetf(ios::left);
coutwidth(50);
cout<<«r=»<<r<<endl
;

 

r=-25.45
boolalphaOutput of logical values in text form 

 

bool a=true;
cout<<a<<endl;
coutsetf(ios::boolalpha);
cout<<a<<endl
;

 

1
true
decOutput values in the decimal numeral system (default) 

 

int r=25;
cout<<"r="<<r<<endl
;

 

r=-25
octOutput of values in the octal number system (for this you need to remove the inference flag in the decimal) 

 

int p=23;
coutunsetf(ios::dec);
coutsetf(ios::oct);
cout<<"p="<<p<<endl
;

 

p=27
hexOutput of values in hexadecimal number system (for this you need to remove the flag output in decimal) 

 

int p=23;
coutunsetf(ios::dec);
coutsetf(ios::hex);
cout<<"p="<<p<<endl
;

 

p=17
showbaseDisplay the base of the number system indicator 

 

 
int r=25;
coutunsetf(ios::dec);
coutsetf(ios::hex| ios::showbase);
cout<<«p=»<<p<<endl
;

 

p=0x17
uppercaseUse uppercase letters in hexadecimal digits 

 

int p=29;
coutunsetf(ios::dec);
coutsetf(ios::hex| ios::uppercase);
cout<<«p=»<<p<<endl
;

 

p=1D
showposDisplay the + sign for positive numbers 

 

int p=29;
coutsetf(ios::showpos);
cout<<«p=»<<p<<endl
;

 

p=+29
scientificExponential form of output of real numbers 

 

double p=146.673;
coutsetf(ios::scientific);
cout<<«p=»<<p<<endl
;

 

p=1.466730e+002
fixedFixed form of real number output (default) 

 

double p=146.673;
coutsetf(ios::fixed);
cout<<«p=»<<p<<endl
;

 

p=146.673

 

Flags are useful when you want to change the settings of all subsequent I/O operators. Using more flags to control a single I/O statement is not entirely convenient. Another way to format is to use manipulators directly in the cin and cout operators.

 

Using formatting manipulators

 

The manipulators are built directly into the I/O operators. You're already familiar with one of them (endl). The following table lists the main formatting manipulators with examples. To use them correctly, you need to connect the iomanip library using the #include <iomanip operator>

 

ManipulatorDescriptionExample of useResult
setw(n)Specifies the width of the output field in n characters 

 

int r=253;
coutsetf(ios::fixed);
cout<<«r=»<<setw(10)<<r<<endl
;

 

r=253
setprecision(n)Specifies the number of digits (n-1) in the fractional part of a number 

 

double p=1234.6578;
coutsetf(ios::fixed);
cout<<"p="<<setw(15)<<setprecision(3)<<p<<endl
;

 

p=1234.658
leftLeft alignment (default) 

 

int r=25;
coutwidth(15);
cout<<«r=»<<setw(15)<<left<<r<<endl
;

 

r=-25
rightRight alignment 

 

int r=25;
coutwidth(15);
cout<<«r=»<<setw(15)<<right<<r<<endl
;

 

r=-25
boolalphaOutput of logical values in text form 

 

bool a=true;
cout<<boolalpha<<a<<endl
;

 

true
noboolalphaInference of logical quantities in numerical form 

 

bool a=true;
cout<<noboolalpha<<a<<endl
;

 

1
decOutput values in the decimal numeral system (default) 

 

int r=0253;
cout<<"r="<<dec<<r<<endl
;

 

r=171
octOutput of values in the octal number system (for this you need to remove the inference flag in the decimal) 

 

int r=253;
cout<<"r="<<oct<<r<<endl
;

 

r=375
hexOutput of values in hexadecimal number system (for this you need to remove the flag output in decimal) 

 

int r=253;
cout<<"r="<<hex<<r<<endl
;

 

r=fd
showbaseDisplay the base of the number system indicator 

 

int p=253;
cout<<"p="<<hex<<uppercase<<showbase<<p<<endl
;

 

p=0XFD
noshowbaseDo not display the base of the number system indicator 

 

int p=253;
cout<<"p="<<hex<<nouppercase<<showbase<<p<<endl
;

 

p=FD
uppercaseUse uppercase letters in hexadecimal digits 

 

int p=253;
cout<<"p="<<hex<<uppercase<<p<<endl
;

 

p=FD
nouppercaseUse lowercase letters in hexadecimal digits 

 

int p=253;
cout<<"p="<<hex<<nouppercase<<p<<endl
;

 

p=fd
showposDisplay the + sign for positive numbers 

 

int p=29;
cout<<«p=»<<showpos<<p<<endl
;

 

p=+29
noshowposDo not display the + sign for positive numbers 

 

int p=29;
cout<<«p=»<<noshowpos<<p<<endl
;

 

p=29
scientificExponential form of output of real numbers 

 

double p=146.673;
cout<<«p=»<<scientific<<p<<endl
;

 

p=1.466730e+002
fixedFixed form of real number output (default) 

 

cout<<«p=»<<fixed<<p<<endl;

 

p=146.673
setfill(c)Set the c character as a placeholder 

 

cout<<"x="<<right<<setw(10)<<setprecision(4)<<setfill('!' )<<(float) 1/7<<endl;
cout<<"x="<<left<<setw(10)<<setprecision(4)<<setfill('!' )<<(float) 1/7<<endl
;

 

x=!!!! 0.1429 x=0.1429!!!!

Other ways to control the width of the output field using operators are:

  • cout.width(n) — sets the width of the output field to n positions;
  • cout.presicion(m) — defines digits in the fractional part of the number;

When using the cin and cout operators, the text file is actually I/O. When you type, the text file is the keyboard, and when you type, the display screen. Cin and cout are actually the names of threads (we'll talk about them in the following tutorials) that are responsible for entering and outputting to a text file. Therefore, many of the formatted I/O features discussed will be used in the processing of text files.