Unit-6: Darshan Institute of Engineering & Technology For Diploma Studies

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Darshan Institute of Engineering & Technology for Diploma Studies Unit-6

1. C++ Stream Classes


Class Name Contents
ios (General • Contains basic facilities that are used by all other input and output classes.
I/O stream • Also contains a pointer to a buffer object.
class) • Declares constants and functions that are necessary for handling formatted
input and output functions.
istream (Input • Inherits the properties of ios.
stream) • Declares input functions such as get(), getline() and read()
• Contains overloaded extraction operator >>
ostream • Inherits the properties of ios.
(output stream) • Declares output functions such as put() and write()
• Contains overloaded insertion operator <<
iostream (I/O • Inherits the properties of ios, istream and ostream through multiple
stream) inheritance and thus contains all the input and output functions.
streambuf • Provides an interface to physical devices through buffers.
• Acts as a base for filebuf class used ios files.

2. Unformatted I/O Operations.


• C++ language provides a set of standard built-in functions which will do the work of reading and
displaying
data or information on the I/O devices during program execution.
• Such I/O functions establish an interactive communication between the program and user.
Function Syntax Use
cout cout<<” ”<<” ”; To display character, string and number on output
device.
Cin cin>> var1>>var2; To read character, string and number from input device.
get(char*) char ch; To read character including blank space, tab and
cin.get(ch);
newline character from input device. It will assign input
character to its argument.
get(void) char ch; To read character including blank space, tab and
ch=cin.get();
newline character from input device. It will returns
input character.
put() char ch; To display single character on output device. If we use
cout.put(ch);
a number as an argument to the function put(), then it
will convert it into character.
getline() char name[20]; It is used to reads a whole line of text that ends with a
int size=10;
newline character or size -1 character. First argument
cin.getline(name,size);
represents the name of string and second argument
indicates the number of character to be read.
write() char name[20]; It is used to display whole line of text on output device.
int size=10;
First argument represents the name of string and second
cout.write(name,size);
argument indicates the number of character to be
display.
• Example of cin and cout:
#include <iostream.h>
int main()
{
int a;
cout<<"Enter the number";
cin>>a;
cout<<"The value of a="<<a;

1 Dept: CE Programming In C++ (3330702) Nitin Rola


 
Darshan Institute of Engineering & Technology for Diploma Studies Unit-6

return 0;
}

• Example of get(char*), char(void) and put():


#include <iostream.h>
int main()
{
int a=65;
char ch;
cin.get(ch); //get(char*)
cout.put(ch); //put()
ch=cin.get(); //get(void)
cout.put(ch);
cout.put(a);
return 0;
}

• Example of getline() and write():


#include <iostream.h>
int main()
{
int size=5;
char name[50];
cin.getline(name,size); //getline()
cout.write(name,size); //write
return 0;
}

3. 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
ios::right ios::adjustfield
Right justified output

2 Dept: CE Programming In C++ (3330702) Nitin Rola


 
Darshan Institute of Engineering & Technology for Diploma Studies Unit-6

Padding after sign or base ios::internal ios::adjustfield


indicator (like +##20)
Scientific notation ios::scientific ios::floatfield
ios::fixed ios::floatfield
Fixed point notation
Decimal base ios::doc ios::basefield
ios::oct ios::basefield
Octal base
ios::hex ios::basefield
Hexadecimal base

• 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
ios::uppercase Show trailing decimal point and zeros.
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.h>
#include <math.h>
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);
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

3 Dept: CE Programming In C++ (3330702) Nitin Rola


 
Darshan Institute of Engineering & Technology for Diploma Studies Unit-6

+...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.h>
#include <iomanip.h>
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);
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 Dept: CE Programming In C++ (3330702) Nitin Rola


 

You might also like