0% found this document useful (0 votes)
135 views4 pages

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

The document discusses C++ stream classes and input/output operations. It describes the ios, istream, ostream and iostream classes which provide basic input/output functionality. It also covers unformatted I/O functions like cout, cin, get(), put() and getline() for reading and writing basic data types. The document then explains formatted I/O using ios class functions, manipulators and user-defined functions to control formatting of output like width, precision and justification. Examples are provided to demonstrate the use of different stream classes and I/O functions.

Uploaded by

nikunj_npb1991
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
135 views4 pages

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

The document discusses C++ stream classes and input/output operations. It describes the ios, istream, ostream and iostream classes which provide basic input/output functionality. It also covers unformatted I/O functions like cout, cin, get(), put() and getline() for reading and writing basic data types. The document then explains formatted I/O using ios class functions, manipulators and user-defined functions to control formatting of output like width, precision and justification. Examples are provided to demonstrate the use of different stream classes and I/O functions.

Uploaded by

nikunj_npb1991
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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>> var1>>var2;
Cin
To read character, string and number from input device.
char
ch;
get(char*)
To read character including blank space, tab and
cin.get(ch);
newline character from input device. It will assign input
character to its argument.
char
ch;
get(void)
To read character including blank space, tab and
ch=cin.get();
newline character from input device. It will returns
input character.
char ch;
put()
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.
char name[20];
getline()
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.
char name[20];
write()
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;

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);
cout.write(name,size);
return 0;
}

//getline()
//write

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
cout.width(size);
width()
To specify the required field size for displaying an
output value.
cout.precision(2);
precision()
To specify the number of digits to be displayed after the
decimal point of a float value.
cout.fill('character'); To specify a character that is used to fill the unused
fill()
portion of a field.
cout.setf(arg1,
arg2);
setf()
To specify format flags that can control the form of
output display such as left or right justification.
cout.resetiosflags()
unsetf()
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)
ios::left
ios::adjustfield
Left justified output
ios::right
ios::adjustfield
Right justified output

Dept: CE

Programming In C++ (3330702)

Nitin Rola

Darshan Institute of Engineering & Technology for Diploma Studies


Padding after sign or base
indicator (like +##20)
Scientific notation
Fixed point notation
Decimal base
Octal base
Hexadecimal base

Unit-6

ios::internal

ios::adjustfield

ios::scientific
ios::fixed

ios::floatfield
ios::floatfield

ios::doc
ios::oct
ios::hex

ios::basefield
ios::basefield
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.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

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 */

Dept: CE

Programming In C++ (3330702)

Nitin Rola

You might also like