C程序設(shè)計教學課件:CHAPTER 12 THE C INPUT OUTPUT CLASS HIERARCHY_第1頁
C程序設(shè)計教學課件:CHAPTER 12 THE C INPUT OUTPUT CLASS HIERARCHY_第2頁
C程序設(shè)計教學課件:CHAPTER 12 THE C INPUT OUTPUT CLASS HIERARCHY_第3頁
C程序設(shè)計教學課件:CHAPTER 12 THE C INPUT OUTPUT CLASS HIERARCHY_第4頁
C程序設(shè)計教學課件:CHAPTER 12 THE C INPUT OUTPUT CLASS HIERARCHY_第5頁
已閱讀5頁,還剩57頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、C+ Programming CHAPTER 12 THE C+ INPUT/OUTPUT CLASS HIERARCHY112.1 Overview12.2 The High-Level Input/Output Classes 12.3 Manipulators12.4 The File Input/Output Classes12.5 The Character Stream Input/Output Classes212.1 OverviewInput and output facilities are not part of C+ language but instead of fu

2、rnished through a class library.In C+ input and output, a central object is the stream, which is a sequence of bytes. There is a common base class for two derived stream classed basic_istream, an input stream class, and basic_ostream, an output stream class.312.1 OverviewThe standard input/output hi

3、erarchios_basebase_iosbasic_istreambasic_ostreambasic_istringstreambasic_iostreambasic_ostringstreambasic_stringstreambasic_fstreambasic_ifstreambasic_ofstream412.1 OverviewSeveral input/output headers and describes their purpose.HeaderPartial DescriptioniosfwdContains forward declarationsiostreamDe

4、clares cin, cout, etc.iosDeclares ios_base and basic_iosstreambufDeclares basic_streambufistreamDeclares basic_istream ostreamDeclares basic_ostreamiostreamDeclares basic_istream and basic_ostreamiomanipDeclares parameterized manipulatorssstreamDeclares basic_stringbuf and the stringstream classesfs

5、treamDeclares basic_filebuf and the fstream classes512.2 The High-Level Input/Output ClassesIn C+, we use the classes basic_istream, basic_ostream, and basic_iostream to get a high-level interface to input and output.HeaderPartial DescriptioniostreamDeclares basic_istream and basic_iostreamostreamDe

6、clares basic_ostreamistreamDeclares basic_istream 612.2 The High-Level Input/Output Classesbasic_istreambasic_istreamcinvalcin reads the stream, without white space, and save it to the val.Example:int a; char c; char ca20;cinacca;712.2 The High-Level Input/Output Classesbasic_istreamExample:#include

7、 void main()int n; cinn;char ch; cinch;float pi; cinpi;char str20;cinstr;coutn=nendl;coutch=chendl;coutpi=piendl;coutstring=str ( signed char);istream & operator (unsigned char);istream & operator (short);istream & operator (unsigned short);istream & operator (int);istream & operator (unsigned int);

8、istream & operator (long);istream & operator (unsigned long);istream & operator (float);istream & operator (double);istream & operator (long double);912.2 The High-Level Input/Output Classesbasic_istreamMethodsA) get( );When get() is invoked, the next character in the stream, white space or not, is

9、returned.Example:int c;while( ( c=cin.get( ) )!=EOF ) coutc;1012.2 The High-Level Input/Output Classesbasic_istreamB) getline( char_type* b, streamsize s, char_type d );b: an array, into which to write characters: an integer value that bounds the number of charactersd: an end-of-line marker1112.2 Th

10、e High-Level Input/Output Classesbasic_istreamMethod getline reads characters until itReaches end-of-fileEncounters an end-of-line markerStores s-1 characters 12Example:#include #include using namespace std;void main (void) char city80; char country80; int i; for (i = 0; i 2; i+) cin.getline(city,80

11、,); cin.get(country,80,n); cout City: city Country: country endl; cin.clear(); cin.ignore( numeric_limits:max(), n ); Input:Beijing,ChinaShanghai,ChinaOutput:City: Beijing Country: ChinaCity: Shanghai Country: China1312.2 The High-Level Input/Output Classesbasic_istreamC) read( char_type* a, streams

12、ize n);Method read reads characters and stores them in the array a until n characters are read or end-of-file occurs.1412.2 The High-Level Input/Output Classesbasic_ostreambasic_ostreamcoutvalcout puts the stream, in val, to the screen.Example:cout“Hello:”123endl;1512.2 The High-Level Input/Output C

13、lassesbasic_ostreamExample:#include void main()float pi=3.14159;coutpi=;coutpi;coutendl;Output:pi=3.141591612.2 The High-Level Input/Output Classesbasic_ostreamOperator Overloadedostream & operator ( signed char);ostream & operator (unsigned char);ostream & operator (short);ostream & operator (unsig

14、ned short);ostream & operator (int);ostream & operator (unsigned int);ostream & operator (long);ostream & operator (unsigned long);ostream & operator (float);ostream & operator (double);ostream & operator (long double);1712.2 The High-Level Input/Output Classesbasic_ostreamMethodsA) put( char_type )

15、;Method put writes the character passed to the output stream.Example:cout.put( c );1812.2 The High-Level Input/Output Classesbasic_ostreamB) write( const char_type* a, streamsize m);Method write writes m characters from the array a to the output stream.1912.2 The High-Level Input/Output Classesbasic

16、_ostreamExample:#include #includevoid main( )char* pc=this is a test!;cout.put(A);cout.put(n);cout.write(pc,strlen(pc); Output:Athis is a test!2012.2 The High-Level Input/Output Classesbasic_iostreambasic_iostreamClass basic_iostream is publicly inherited from both basic_istream and basic_ostream.21

17、12.2 The High-Level Input/Output Classesbasic_iostreamExample:class Complexpublic:Complex( )real=0.0;imag=0.0;Complex(double r)real=r;imag=0.0;Complex(double r, double i)real=r;imag=i;Complex operator + (const Complex& c);Complex operator - (const Complex& c);Complex operator * (const Complex& c);Co

18、mplex operator / (const Complex& c);friend ostream& operator (istream& In, Complex& x);protected:double real, imag;2212.2 The High-Level Input/Output Classesbasic_iostreamostream& operator(ostream& Out, Complex& x)Outx.real+x.imagi(istream& In, Complex& x)Inx.realx.imag;return In;/2312.2 The High-Le

19、vel Input/Output Classesbasic_iostreamvoid main()Complex a(1,2),b(3,4),c;c=a+b;coutc;Output:4+6i2412.3 ManipulatorsA manipulators is a function that either directly or indirectly modifies a stream.HeaderPartial DescriptioniomanipDeclares parameterized manipulators2512.3 ManipulatorsSeveral manipulat

20、ors with arguments are predefined. ManipulatorActs OnPurposesetBase( int n )basic_ostreamSet integer base to n(0 means default)setfill(char_type c)basic_ostreamSet fill character to csetprecision( int n )basic_ostreamSet precision to nsetw( int n )basic_ostreamSet field width to nsetiosflags( mask )

21、ios_baseSet specified format bitsresetiosflags( mask ) ios_baseClear specified format bits2612.3 ManipulatorssetBaseExample:coutsetbase(8) 9endl;Output:112712.3 ManipulatorssetwExample:#include #include using namespace std;void main() double values =1.23,35.36,653.7,4358.24;char *names =Zoot,Jimmy,A

22、l,Stan;for(int i=0;i4;i+)coutsetw(6)namesi setw(10)valuesi endl; Output: Zoot 1.23 Jimmy 35.36 Al 653.7 Stan 4358.246 106 characters2812.3 ManipulatorssetfillExample:#include #include using namespace std; void main() double values =1.23,35.36,653.7,4358.24; for(int i=0; i4; i+) cout.width(10); couts

23、etfill(*)valuesin; Output:*1.23*35.36*653.7*4358.24Fill with *2912.3 Manipulatorssetiosflags and rsetiosflagsExample:#include #include using namespace std; void main() double values=1.23,35.36,653.7,4358.24; char *names=Zoot,Jimmy,Al,Stan; for(int i=0;i4;i+) coutsetiosflags(ios:left) setw(6)namesi r

24、esetiosflags(ios:left) setw(10)valuesi endl;/ flush left/ remove flush leftOutput:Zoot 1.23Jimmy 35.36Al 653.7Stan 4358.2430setprecisionExample:#include #include using namespace std; void main() double values=1.23,35.36,653.7,4358.24; char *names=Zoot,Jimmy,Al,Stan; coutsetiosflags(ios:scientific);

25、for(int i=0;i4;i+) coutsetiosflags(ios:left) setw(6)namesi resetiosflags(ios:left) setw(10)setprecision(1) valuesiopen(filename,iosmode); 3412.4 The File Input/Output Classesbasic_ofstreamExample:#include fstream.h#include iostream.hvoid main()char buf80;ofstream out(out.txt);while (cin.getline(buf,

26、80, ) coutbufendl;outbufopen(filename,iosmode); 3712.4 The File Input/Output Classesbasic_ifstreamExample:ifstream fin( “data.in” );if( !fin )cerr”Cant open data.inn”;Example:ifstream fin;fin.open( “data.in” );3812.4 The File Input/Output Classesbasic_ifstreamExample:#include fstream.h #include iost

27、ream.hvoid main()int line=1;char buf80;ifstream in(in.txt);ofstream out(out.txt);while (in.getline(buf,80) coutline+:bufendl;outbufopen(filename,iosmode); 4112.4 The File Input/Output Classesbasic_fstreamExample:fstream finout( “data.txt” );In this case the file data.txt is for both input and output

28、.42#include /for file streams#include using namespace std;const int MAX = 100; /size of bufferint buffMAX; /buffer for integersint main() /fill buffer with data for(int j=0; jMAX; j+) buffj = j; /(0, 1, 2, .) /create output stream ofstream os(edata.txt, ios:binary); /write to it os.write( (char*)buf

29、f, MAX*sizeof(int) ); os.close(); /must close itWrite data43 for(j=0; jMAX; j+) /erase buffer buffj = 0; /create input stream ifstream is(edata.txt, ios:binary); /read from it is.read(char*)(buff), MAX*sizeof(int) ); for(j=0; jMAX; j+) /check data if( buffj != j ) cerr Data is incorrectn; return 1;

30、cout Data is correctn; return 0; Read data44Output: data.txt ! # $ % & ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; ? A B C D E F G H I J K L M N O P Q R S T U V W X Y Z _ a b c 4512.4 The File Input/Output ClassesMethodsMethodsbasic_ofstream:1 open2 close3 tellp: Gets the value for the streams put point

31、er.4612.4 The File Input/Output Classesbasic_fstream4 seekp( streampos pos );5 seekp( streamoff off, ios:seek_dir dir );pos:The new position value; streampos is a typedef equivalent to long.off:The new offset value; streamoff is a typedef equivalent to long.dir: The seek direction specified by the e

32、numerated type ios: seek_dir, with values including: ios: beg Seek from the beginning of the stream. ios: cur Seek from the current position in the stream. ios: end Seek from the end of the stream.4712.4 The File Input/Output Classesbasic_fstreambasic_ifstream:1 open2 close3 get4 getline5 read6 tell

33、g: Gets the value for the streams get pointer.4812.4 The File Input/Output Classesbasic_fstream7 seekg(streampos pos)8 seekg(streamoff off, ios:seek_dir dir) pos: The new position value; streampos is a typedef equivalent to long. off: The new offset value; streamoff is a typedef equivalent to long.

34、dir: The seek direction. Must be one of the following enumerators: ios:beg Seek from the beginning of the stream. ios:cur Seek from the current position in the stream. ios:end Seek from the end of the stream.49Example:1#include /for file streams#include using namespace std;class person /class of per

35、sonsprotected:char name80; /persons nameint age; /persons agepublic:void getData() /get persons datacout name;cout age;void showData(void) /display persons datacout n Name: name;cout n Age: age;50int main() char ch; person pers; /create person object fstream file; /create input/output file file.open

36、(GROUP.DAT, ios:app | ios:out | ios:in | ios:binary ); do /data from user to file cout nEnter persons data:; pers.getData(); /get one persons data file.write( reinterpret_cast(&pers), sizeof(pers) ); /write to file cout ch; while(ch=y); /quit on nreinterpret_cast(&pers)?51 file.seekg(0); /reset to s

37、tart of file /read first person file.read( reinterpret_cast(&pers), sizeof(pers) ); while( !file.eof() ) /quit on EOF cout nPerson:; /display person pers.showData(); /read another person file.read( reinterpret_cast(&pers), sizeof(pers) ); cout endl; return 0;52Example:2int main() person pers; /creat

38、e person object ifstream infile; /create input file infile.open(GROUP.DAT, ios:in | ios:binary); /open file infile.seekg(0, ios:end); /go to 0 bytes from end int endposition = infile.tellg(); /find where we are int n = endposition / sizeof(person); /number of persons cout nThere are n persons in file;53 cout n; int position = (n-1) * sizeof(person); /number times size infile.seekg(position); /bytes from start /read one person infile.read( reinterpret_cast(&pers), sizeof(pers) ); pers.showData(); /display the person cout endl; retur

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論