In this section we will learn how to control the information flow. There is a set of format flags that control the way information is formatted.
Formatted
I/O
•
In this section we will learn how to
control the information flow. There is a set of format flags that control the
way information is formatted. The ios class declares the values that are used
to set or clear the format flags ‒
•
Following are the format flags along with their meaning.
Flags : Meaning
Skipws:
If this flag is set then leading white spaces are discarded (skipped). On
clearing this flag the leading white spaces are not discarded.
left:
On setting this flag the output becomes left justified.
right:
On setting this flag the output becomes right justified.
internal:
If it is set then numeric value is padded to fill the field between any sign or
base character and number.
oct:
It is set means numeric values are outputted in octal.
dec:
It is set means numeric values are outputted in decimal.
hex:
It is set means numeric values are outputted in hex.
showbase:
If set then it shows base indicator(for example 0X for hex).
showpos:
It shows+ sign before positive numbers.
showpoint:
On set it shows decimal point and trailing zeros for all floating point numbers.
Scientific:
On set it shows exponential format on floating point numbers.
fixed: On set it shows fixed
format on floating point numbers.
boolalpha:
Booleans can be set using true or false.
uppercase:
On setting this flag upper case A‒F are used for hex values and E for
scientific values.
•
The oct, dec and hex fields are collectively referred as basefield. The left, right
and internal fields can be referred
as adjustfield, similarly scientific and fixed can be referred as
floatfield.
•
The setf() function is used to set
the flag and unsetf() function is
used to clear the flag.
•
For example, if we want the text to be displayed on the console should be right
justified then ‒
cout.setf(ios::right);
cout<<"Twinkle
Twinkle Little Stars";
Following
is a program that shows how several format flags can be used.
#include <iostream>
using namespace std;
int main()
{
cout << 100.75
<< "hello India" << 100 << "\n";
tortuot cout <<
10 << " " << ‒10 << "\n";
cout << 100.0
<< "\n\n";
cout.unsetf(ios::dec);
cout.setf(ios::hex| ios::scientific);
cout << 100.75
<< "hello India<< 100 << "\n";
cout.setf(ios::showpos);
cout << 10
<< " " << ‒10 << "\n";
cout.setf(ios::showpoint
| ios::fixed);
cout << 100.0;
retrun 0;
}
100.75 hello India 100
10‒10
100
1.0075e+02 hello India
64
a fffffff6
+100.000000
There
are three important member functions that define the fields like width,
precision and fill characters.
•
By default the output value contains the
number of characters as per the space occupied by it. But we can define the
width of the output by using width() function.
cout.width(10);
cout<<"ABCD"
•
It will set minimum field width of 10.
That means in a line total number of bobo characters are 10. In above cout
statement the text "ABCD" contains 4 characters only. Hence first 6
blank characters and then ABCD will be printed on the output screen. The output
will be
ABCD
That
means we get the right justified output.
•
Following program illustrates the use of
width, precision and fill functions.
#include<iostream>
using namespace std;
int main()
{
cout << "ABCD "<<"\n";
cout.width(10);
cout<<"ABCD "<<"\n"; //sets
right justified text
cout.fill('*'); // set fill character
cout.width(10); // set width
cout<<"ABCD"<<"\n";
// right justified text with preceding *hence the total number
of characters are 10
cout.setf(ios::left); // left justify
cout.width(10); // set width
cout<<"ABCD"<<"\n"; // output
left justified
cout.width(10); // set width
cout.precision(10); //set 10 digits of precision
cout << 10.203040 << "\n";
indacout.precision(4); // set 4 digits of precision
cout << 102.0304050 << "\n";
cout.width(10);
cout.fill('*');
cout <<""; //simply prints * 10 times
return 0;
}
ABCD
ABCD
******ABCD
ABCD******
10.20304**
102.0304
**********
Object Oriented Programming: Chapter 6: IO Systems and File IO : Tag: Oops, Computer Programming : C++ Object Oriented Programming - Formatted I/O
Object Oriented Programming (OOPs)
CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation
English Essentials II
EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Tamils and Technology தமிழர்களும் தொழில்நுட்பமும்
UC25H02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Linear Algebra
MA25C02 2nd Semester | 2025 Regulation
Applied Physics (CSIE) II
PH25C03 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Digital Principles and Computer Organization
CS25C06 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Basic Electrical and Electronics Engineering
EE25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Object Oriented Programming (OOPs)
CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation
Re-Engineering for Innovation
ME25C05 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Object Oriented Programming (OOPs) - Laboratory
CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation