Vous êtes sur la page 1sur 17

I/O Systems & File Processing

by…
kharadi fahad
c++ stream classes
IOS

istream iostream ostream

get() put()
getline() write()
read() --------------
---------------- Overload ‘>>’
Overload ‘<<’
Types of i/o operation
I/O OPERATION

UNFORMATTED FORMATTED

IOS CLASS USER DEFINED


MANIPULATORS
FUNCTION OUTPUT
put() , get() FUNCTION
getline() ,
write()
-----------------------------------------
------------------------------ Width(),precision() setw(),setprecision(),
Overload ,fill(),setf() setfill(),setioflags()
Overload oper.
oper. ,unsetf() ,resetioflags()
‘<<’
‘<<’ ,, ‘>>’
‘>>’
unformatted
1>get():- • 2>put() :-
eg:- # include<iostream.h>
# include<iostream.h> void main()
void main() {
{ char c;
char c; cin.get(c);
cin.get(c); while(c!='\n')
while(c!='\n') {
{ cin,put(c);
cout<<c; cin.get(c);
cin.get(c); }
} }
}
3>getline():-
Syntax: cin.getline(line,size); Output 1
#include<iostream.h>
#include<conio.h> enter the line-->hellooops
void main() city name-->hellooops
{ enter the new line-->
clrscr(); enter again the new line-->hello fahad
int size=20;
char c[20]; the entered line is-->hello fahad
cout<<"enter the line-->" ;
cin>>c;
cout<<"city name-->"<<c;
Output 2
cout<<"\nenter the new line-->";
cin.getline(c,size); enter the line-->hello oops
cout<<c; city name-->hello
cout<<"\nenter again the new line-->"; enter the new line--> oops
cin.getline(c,size);
enter again the new line-->hello fahad
cout<<"the entered line is-->"<<c;
getch(); the entered line is-->hello fahad
}
4>write():-
Syntax: cin.write(line,size);
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
char c[20];
Output 
cout<<“enter the line”;
enter the line-->hello oops
cin.getline(c,20); the entered line is-->hello oops
cout<<“the entered line is”;
cout.write(c,20);
getch();
}
formatted
IOS CLASS FUNCTION MANIPULATORS

width(int w) setw(int w)

precision(int d) setprecision(int d)

fill(char c) setfill(char c)

setf(arg1,arg2) setioflags(arg1,arg2)

unsetf() resetioflags()

All above are used with cout.____


IOS CLASS FUNCTION
• 1>width(int w):-
Eg1:- Eg2:-
cout.width(6); cout.width(6);
cout<<361; cout<<361;
cout.width(5); cout<<18;
cout<<18;
OUTPUT:- OUTPUT:-

3 6 1 1 8 3 6 1 1 8
2> precision(int d);-
Eg:-
cout.precision(4);
cout<<sqrt(2)<<‘\n’;
cout<<3.14159<<‘\n’;
cout<<2.50003<<‘\n’;
OUTPUT:-
1.4112TRUCATED VALUE.
3.1416ROUNDED TO NEAREST VALUE.
2.5 NO TRAILING ZEROS
• 3> fill(char c):-
Eg:-
cout.fill(‘ * ‘);
cout.width(10);
cout<<1265;
OUTPUT:-

* * * * * * 1 2 6 5
4>Formattingflags,Bit fields:-
setf(arg1,arg2):-
Format arg1 arg2
Left-justified o/p ios::left ios::adjustfield
Right-justified o/p ios::right ios:: adjustfield
Padding after base ios::internal ios:: adjustfield
or sign indicator
Scientific notation ios::scientific ios::floatfield
Fixed point notation ios::fixed ios:: floatfield
Decimal base ios::dec ios::basefield
Octal base ios::oct ios:: basefield
Hexadecimal base ios::hex ios:: basefield
Eg:-
cout.fill(‘*’);
cout.setf(ios::left,ios::adj
ustfield);
cout.width(12);
cout<<“oops”<<‘\n’;
OUTPUT:-

o o p s * * * * * * * *
• To display following
341.32 3 4 1 3 2

29.00 2 9

19.230 1 9 2 3
Other arg1 Description
setf(ios::showpoint) display a trailing decimal point and trailing
decimal zeros when real numbers are
displayed
setf(ios::showpos) display a + character with positive values.

setf(ios::showbase) to display the numeric base of integral values.


With hexadecimal values the 0x prefix is used,
with octal values the prefix 0.

setf(ios::uppercase) use capital letters in the representation of


(hexadecimal or scientifically formatted)
values
setf(ios::skipws) leading white space characters (blanks, tabs,
newlines, etc.) are skipped

setf(ios::unitbuf) flush the stream after each output operation.

setf(ios::stdio) flush the standard C streams stdout and stderr


after each output operation.
Eg:-
cout.setf(ios::showpos);;
cout.setf(ios::showpoint);
cout.precision(4);
cout.setf(ios::fixed,ios::floatfield);
cout.setf(ios::internal,ios::adjustfield);
cout.width(10);
cout<<120.3<<’\n’;
OUTPUT:-
+ 1 2 0 . 3 0 0 0
User defined manipulator
Syntax:
ostream & manipulator(ostream & output)
{
---------
---------
return output;
}
Eg:-
ostream & oops(ostream & output)
{
output<<“oops!!!!”;
return output;
}
main()
{
cout<<“hello”<<oops<<300;
}
OUTPUT:-
hello oops!!!! 300

Vous aimerez peut-être aussi