1 C/C++ C FILE* fscanf fgets fread fprintf fputs fwrite C++ ifstream ofstream >> << 2 1
(iostream) ios: istream: ostream: ifstream: ofstream: fstream: 3 C++ ios istream ostream iostream ifstream fstream ofstream 4 2
cin : istream cout : ostream cerr : ostream clog : ostream 5 ios (flag) ios (flag) ios setf() unsetf() ios:: skipws left right dec oct hex showbase upercase showpos scientific fixed 6 3
ios #include <iostream.h> void main(){ cout<<12345.6<<endl; // 1.23456E+04 cout.setf(ios::hex); cout.setf(ios::fixed); cout<< 100<<endl; // 64 cout<<12345.6<<endl; cout.setf(ios::showbase); // 12345.6 cout << 100<<endl; // 0x64 cout.setf(ios::uppercase); cout << 255<<endl; // 0XFF cout.unsetf(ios::hex); cout.setf(ios::showpos); cout<<100<<endl; // +100 cout.setf(ios::scientific); 7 ios ws dec oct hex endl ends flush setw(int) setfill(int) setprecision(int) setiosflags(long) resetiosflags(long) #include <iomanip.h> 8 4
ios #include <iomanip.h> 12 void main(){ Computer cout<<setw(5)<<12<<endl; *******Computer cout<<setw(15)<<"computer"<<endl; 3.142 cout<<setfill('*'); 64 144 cout<<setw(15)<<"computer"<<endl; cout<<setprecision(3)<<3.1415926535<<endl; cout<<hex<<100<<endl; cout<<oct<<100<<endl; 9 ios ch=fill() fill(ch) p=precision() precision(p) w=width() width(w) setf(flags) unsetf(flags) setf(flags) 10 5
istream >> get(ch) ch getstr(str) str get(str,max) str MAX get(str,max,delim) str MAX DELIM DELIM getline(str,max,delim ) str MAX DELIM DELIM peek(ch) count=gcount() get getline read byte read(str,max) MAX byte str seekg() seekg(pos,seek_dir) pos=tellg(pos) 11 ostream << put(ch) ch flush() write(str,size) str SIZE byte seekp(position) seekp(position,seek_dir) pos=tellp() 12 6
goodbit ( ) 0x00 eofbit 0x01 failbit 0x02 badbit 0x04 hardfail 0x08 13 int eof() int fail() int bad() int good() clear(int) EOF failbit badbit hardfail badbit hardfail 14 7
#include <iostream.h> void main(){ int i; cout <<"i="; cin >> i; cout <<"Good ="<<cin.good()<<"\n"; cout <<"Eof ="<<cin.eof()<<"\n"; cout <<"Fail ="<<cin.fail()<<"\n"; cout <<"Bad ="<<cin.bad()<<endl; i=5 Good=1 Eof =0 Fail =0 Bad =0 i=a Good=0 Eof =0 Fail =2 Bad =0 15 / ifstream: ofstream: fstream : Step 1: Step 2: Step 3: 16 8
#include <fstream.h> 17 : ifstream ofstream ostream fout( data,ios::out); : open ifstream fin; fin.open( data,ios::in) 18 9
open in out ate (AT End) app (Append) trunc (Truncate) nocreate noreplace binary 19 istream seekg(long, ) ostream seekp(long, ) ios::beg ios::cur ios::end long tellg() long tellp() 20 10
ifstream ( ) #include <iostream.h> #include <fstream.h> void main(){ char fname[20]; cout<<"file name="; cin >>fname; // ifstream fin(fname,ios::in ios::binary); // fin.seekg(0l,ios::end); // cout<<"file length="<<fin.tellg()<<endl; 21 : 0x1a ^Z : DOS 0x0D 0x0A ^M(CR) ^J(LF) C/C++ 0x0A ^J(LF) cin >> getline get cout << put 22 11
#include <iostream.h> #include <fstream.h> const int linelen=200; void main(){ char fname[20]; ifstream fin; ofstream fout; char str[linelen]; cerr<< "From file="; cin >> fname; fin.open( fname,ios::in); cerr<< "To file="; cin >> fname; fout.open( fname,ios::out); while (!fin.eof()){ fin.getline(str,linelen,'\n'); fout <<str<<endl; 23 : ifstream read ofstream write 24 12
cout << cout << obj operator << (cout,obj) ostream& operator<<(ostream &os, & ) 25 void main(){ #include <iostream.h> complex a(2,-1); class complex{ cout <<a; float real; float imag; public: complex(float r=0, float i=0){ 2+ -1i real=r; imag=i; friend ostream& operator <<(ostream&, complex&); ; ostream& operator<<(ostream& os, complex& c){ os << c.real <<"+ "; os << c.imag<<"i"; return os; 26 13
cin >> cin>> obj operator >> (cin,obj) istream& operator>>(istream &is, & ) 27 #include <iostream.h> is >> m.phone; class man{ return is; char name[40]; char phone[10]; ostream& operator<<(ostream& os, public: man &m){ friend istream& os << "Name : "<<m.name<<endl; operator>>(istream&, man&); os << "Phone: "<<m.phone<<endl; friend ostream& returnos; operator<<(ostream&, man&); ; void main(){ istream& operator>>(istream& is, man Tom; man& m){ cin >> Tom; cout <<"Name:"; cout << Tom; is >> m.name; cout << "Phone:"; 28 14