Darshan Institute of Engineering & Technology 140705 – OOP with C++
Computer Engineering Unit - 7 IO and File Management
{
int size=5;
char name[50];
cin.getline(name,size); //getline()
cout.write(name,size); //write
return 0;
}
3) List out and explain functions and manipulators used for Formatted I/O operations.
We can format input and output by following methods.
1. ios class funstions and flags.
2. Manipulators.
3. User-defined output functions.
Now we will see each method in detail.
The ios format functions are shown in below table:
Function Syntax Use
width() cout.width(size); To specify the required field size for displaying an output
value.
precision() cout.precision(2); To specify the number of digits to be displayed after the
decimal point of a float value.
fill() cout.fill('character'); To specify a character that is used to fill the unused portion of
a field.
setf() cout.setf(arg1, arg2); To specify format flags that can control the form of output
display such as left or right justification.
unsetf() cout.resetiosflags() To clear the flags specified.
In setf() we can provide one or two argument.
cout.setf(arg1, arg2);
The arg1 is formatting flags defined in the ios class. And arg2 is known as bit field specifies the group to
which the formatting flags belong.
The flags and bit field are shown below
Format required Flag (arg1) Bit-field (arg2)
Left justified output ios::left ios::adjustfield
Right justified output ios::right ios::adjustfield
Padding after sign or base
ios::internal ios::adjustfield
indicator (like +##20)
Scientific notation ios::scientific ios::floatfield
Fixed point notation ios::fixed ios::floatfield
Decimal base ios::doc ios::basefield
Hardik Doshi, Ishan Rajani 51
Darshan Institute of Engineering & Technology 140705 – OOP with C++
Computer Engineering Unit - 7 IO and File Management
Octal base ios::oct ios::basefield
Hexadecimal base ios::hex ios::basefield
The flags without field bit are shown below
Flag Meaning
ios::showbase Use base indicator on output.
ios::showpos Print + before positive numbers.
ios::showpoint Show trailing decimal point and zeros.
ios::uppercase
Use uppercase letters for hex output.
ios::skipus Skip white space on input.
ios::unitbuf Flush all streams after insertion.
ios::stdio Flush stdout and stderr after insertion.
Example of ios functions:
#include <iostream>
#include <math>
using namespace std;
int main()
{
cout.fill('*');
cout.setf(ios::left,ios::adjustfield);
cout.width(10);
cout<<"value";
cout.setf(ios::right,ios::adjustfield);
cout.width(15);
cout<<"SQRT OF VALUE"<<"\n";
cout.fill('.');
cout.precision(4);
cout.setf(ios::showpoint);
cout.setf(ios::showpos);
cout.setf(ios::fixed,ios::floatfield);
for(int i=1;i<=10;i++)
{
cout.setf(ios::internal, ios::adjustfield);
cout.width(5);
cout<<i;
cout.setf(ios::right, ios::adjustfield);
cout.width(20);
cout<<sqrt(i)<<"\n";
}
cout.setf(ios::scientific, ios::floatfield);
Hardik Doshi, Ishan Rajani 52
Darshan Institute of Engineering & Technology 140705 – OOP with C++
Computer Engineering Unit - 7 IO and File Management
cout<<"\nSQRT(100)="<<sqrt(100)<<"\n";
return 0;
}
Output:
value*******SQRT OF VALUE
+...1.............+1.0000
+...2.............+1.4142
+...3.............+1.7321
+...4.............+2.0000
+...5.............+2.2361
+...6.............+2.4495
+...7.............+2.6458
+...8.............+2.8284
+...9.............+3.0000
+..10.............+3.1623
SQRT(100)=+1.0000e+01
The manipulators are shown in below table:
Manipulators Use
setw() To specify the required field size for displaying an output value.
setprecision() To specify the number of digits to be displayed after the decimal
point of a float value.
setfill() To specify a character that is used to fill the unused portion of a
field.
setiosflags() To specify format flags that can control the form of output display
such as left or right justification.
resetiosflags() To clear the flags specified.
Manipulators are used to manipulate the output in specific format.
Example for manipulators
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
cout.setf(ios::showpoint);
cout<<setw(5)<<"n"
<<setw(15)<<"Inverse of n"
<<setw(15)<<"Sum of terms\n\n";
double term,sum=0;
for(int n=1;n<=10;n++)
{
term=1.0/float(n);
Hardik Doshi, Ishan Rajani 53
Darshan Institute of Engineering & Technology 140705 – OOP with C++
Computer Engineering Unit - 7 IO and File Management
sum=sum+term;
cout<<setw(5)<<n
<<setw(14)<<setprecision(4)
<<setiosflags(ios::scientific)<<term
<<setw(13)<<resetiosflags(ios::scientific)
<<sum<<endl;
}
return 0;
}
Output:
n Inverse of n Sum of terms
1 1.0000e+00 1.0000
2 5.0000e-01 1.5000
3 3.3333e-01 1.8333
4 2.5000e-01 2.0833
5 2.0000e-01 2.2833
6 1.6667e-01 2.4500
7 1.4286e-01 2.5929
8 1.2500e-01 2.7179
9 1.1111e-01 2.8290
10 1.0000e-01 2.9290
4) Explain file stream classes with iostream classes in short.
ios
iostrem file
istream streambuf ostream
iostream
ifstream fstream ofstream filebuf
fstream base fstream file
Hardik Doshi, Ishan Rajani 54