STREAMS IN C++
logo 公司名称
The stream is an intermediator between I/O devices and the user.
Stream is flow of data in bytes in sequence.
Source Stream: When data is received from input devices in sequence
then it is called as source stream
Destination Stream: when the data is passed to output devices then it is
called as destination stream.
logo 公司名称
2
PRE-DEFINED CONSOLE STREAMS:
C++ contains Cin and Cout predefined streams that opens automatically
when a program begins its execution.
Cin represents the input stream connected to the standard input device.
Cout represents the output stream connected to standard output device.
1. cout
2. cin
logo 公司名称
3
The Standard Output Stream (cout):
cout Standard output, usually screen, corresponding to iostream in C+
+. It passes data to output devices such as monitor and printers.
Thus, it controls output.
E-g;
#include<iostream.h>
void main()
{
cout<<“Welcome”;
}
Output:
Welcome
Nov 15, 2024 4 logo 公司名称
4
The Standard Input Stream (Cin)
cin Standard input, usually keyboards, corresponding to iostream in
C++. It handles input from input devices usually from keyboard
E-g;
#include<iostream.h>
void main()
{
int a,b,c;
cout<<“\n Enter Two Numbers to sum:”;
cin>>a>>b
c=a+b;
cout<<“\n The Sum is:”<<c;
}
Output:
Enter Two Numbers to sum:
10
42
Nov 15, 2024 The Sum is: 52 5 logo 公司名称
5
Formatted I/O:
C++ provides various formatted console I/O functions for
formatting the output. They are of three types.
ios class functions
Manipulators
User-Defined Manipulators
B.1 ios class functions:
width() - specify the output width.
precision() - specify No.of digits after decimal point.
fill() - specify the fill character for unused portion of width.
setf() - set the ios flags(dec,oct,hex,etc,.)
unsetf() - clear the flags specified
Nov 15, 2024 6 logo 公司名称
6
1) width():-
Syntax: width(w);
E-g;
Cout<<width(6);
Cout<<2014;
Output:
2 0 1 4
2) precision():-
Syntax: precision(n);
E-g;
Cout<<width(6);
Cout<<precision(2);
Cout<<21.412903;
Output:
2 1 . 4 1
Nov 15, 2024 7 logo 公司名称
7
3) fill()
Syntax: fill(ch);
E-g;
Cout<<fill(‘*’);
Cout.width(6)
Cout<<2014;
Output:
* * 2 0 1 4
4) setf()
Syntax: setf(arg1,arg2);
arg1→formatting flag; arg2→bit-field
E-g;
cout.setf(ios::left,ios::adjustfield);
cout.width(12);
cout<<“TOTAL MARK”;
Output: T O T A L MA R K
logo 公司名称
Nov 15, 2024 8
8
B.2 Manipulators:
Manipulators are operators that are used to format the display.
To access these manipulators, the file iomanip should be included
in the program.
Manipuators Equivalent ios
i.e) #include<iomanip.h>functions
setw() width()
setprecision() precision()
setfill() fill()
setiosflags() setf()
resetiosflags() unsetf()
endl is a manipulator operator-used to insert new line
E-g; int a=12;Cout<<setw(3)<<“a=”<<setw(5)<<a<<endl;
a = 1 2
Nov 15, 2024 9 logo 公司名称
ws: It is defined in istream and is used to ignore the whitespaces in 9
B.3 Custom/User-Defined Manipulators:
The programmer can also define his/her own manipulator
according to the requirements of the program.
Syntax:
ostream & manipulator_name(ostream & obj)
{
//body of the function
return obj;
}
E-g: ostream & tab (ostream & o)
{
o <<"\t";
return o;
}
void main( )
{
cout <<1<<tab<<2 <<tab<<3;
}
OUTPUT
Nov 15, 2024 123 10 logo 公司名称
10
› Code Explanation- a function tab that overloads the
insertion operator (<<) to insert a tab character ("\t")
into an output stream.
› ostream &tab(ostream & o): This is a function that
takes an ostream reference (like cout) as a parameter
and returns a reference to the same stream after
adding a tab character ("\t").
› Inside the function, the tab character (\t) is inserted
into the stream o using the standard << operator, and
then the function returns the stream o, which now
contains the tab.
logo 公司名称
› In the main function, the statement cout << 1
<< tab << 2 << tab << 3; is executed.
› First, cout << 1 outputs the number 1 to the
console.
› Then tab is invoked. Since tab is a function
that overloads the << operator, it inserts a tab
character (\t) into the output stream.
› After that, cout << 2 outputs the number 2,
followed by another tab inserted by tab.
› Finally, cout << 3 outputs the number 3.
logo 公司名称
Thank You