I/O Systems & File Processing by… kharadi fahad
c++ stream classes IOS
istream
get() getline() read() ----------------
Overload ‘<<’
iostream
ostream
put() write() --------------
Overload ‘>>’
Types of i/o operation I/O OPERATION
UNFORMATTED
put() , get() getline() , write() ----------------------------------------------------------------------
Overload Overload oper. oper. ‘<<’ ‘<<’ ,, ‘>>’ ‘>>’
FORMATTED
IOS CLASS FUNCTION
USER DEFINED OUTPUT FUNCTION
Width(),precision() ,fill(),setf() ,unsetf()
MANIPULATORS
setw(),setprecision(), setfill(),setioflags() ,resetioflags()
unformatted 1>get():eg:# include void main() { char c; cin.get(c); while(c!='\n') { cout<
• 2>put() :# include void main() { char c; cin.get(c); while(c!='\n') { cin,put(c); cin.get(c); } }
3>getline():Syntax:
cin.getline(line,size);
#include #include void main() { clrscr(); int size=20; char c[20]; cout<<"enter the line-->" ; cin>>c; cout<<"city name-->"<"; cin.getline(c,size); cout<"; cin.getline(c,size); cout<<"the entered line is-->"<
Output 1 enter the line-->hellooops city name-->hellooops enter the new line--> enter again the new line-->hello fahad the entered line is-->hello fahad
Output 2 enter the line-->hello oops city name-->hello enter the new line--> oops enter again the new line-->hello fahad the entered line is-->hello fahad
4>write():Syntax:
cin.write(line,size);
#include
#include void main() { clrscr(); char c[20]; cout<<“enter the line”; cin.getline(c,20); cout<<“the entered line is”; cout.write(c,20); getch(); }
Output enter the line-->hello oops the entered line is-->hello oops
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:cout.width(6); cout<<361; cout.width(5); cout<<18; OUTPUT:3 6 1
Eg2:cout.width(6); cout<<361; cout<<18; OUTPUT: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.4112TRUCATED VALUE. 3.1416ROUNDED 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 Left-justified o/p ios::left Right-justified o/p ios::right Padding after base or ios::internal sign indicator
arg2 ios::adjustfield ios:: adjustfield ios:: adjustfield
Scientific notation ios::scientific Fixed point notation ios::fixed Decimal base ios::dec Octal base ios::oct Hexadecimal base ios::hex
ios::floatfield ios:: floatfield ios::basefield ios:: basefield 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 29.00
19.230
1
9
3
2
2
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”<
Related Documents
More Documents from ""