c++課件第十三章輸入輸出流(1)_第1頁
c++課件第十三章輸入輸出流(1)_第2頁
c++課件第十三章輸入輸出流(1)_第3頁
c++課件第十三章輸入輸出流(1)_第4頁
c++課件第十三章輸入輸出流(1)_第5頁
已閱讀5頁,還剩31頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

1、.第13章 輸入輸出流13.1 C+的輸入和輸出13.1.1輸入輸出的含義(1) 對(duì)系統(tǒng)指定的標(biāo)準(zhǔn)設(shè)備進(jìn)展輸入和輸出。(2) 以外存磁盤文件為對(duì)象進(jìn)展輸入和輸出。3對(duì)內(nèi)存指定的空間進(jìn)展輸入和輸出。13.1.2 C+的I/O對(duì)C的開展類型平安和可擴(kuò)展性13.1.3 C+的輸入輸出流1. iostream類庫中有關(guān)的類圖13.1圖13.2圖13.32. 與iostream類庫有關(guān)的頭文件iostream fstream strstream stdiostream iomanip3. 在iostream頭文件中定義的流對(duì)象4. 在iostream頭文件中重載運(yùn)算符ostream operator &

2、lt;< char *; /用于向輸出流插入一個(gè)字符串cout<<"C+"相當(dāng)于cout.operator<<"C+"對(duì)于自己的類,可用11章的方法進(jìn)展重載。13.2標(biāo)準(zhǔn)輸出流13.2.1 cout,cerr和clog流1. cout流對(duì)象(1) cout流是流向顯示器的數(shù)據(jù)。(2) 輸出根本類型數(shù)據(jù)時(shí),可以不考慮數(shù)據(jù)的類型是什么。(3) cout流在內(nèi)存中對(duì)應(yīng)開拓了一個(gè)緩沖區(qū),用來存放流中的數(shù)據(jù),當(dāng)向cout流插入一個(gè)endl時(shí),不管緩沖區(qū)是否已滿,都立即輸出流中的所有數(shù)據(jù),然后插入一個(gè)換行符,并刷新流清空緩沖區(qū)。2. c

3、err流對(duì)象cerr流是標(biāo)準(zhǔn)錯(cuò)誤流。cerr流被指定與顯示器關(guān)聯(lián)。cout流通常是傳送到顯示器輸出,但也可以被重定向輸出到磁盤文件。例13.1 解一元二次方程ax2+bx+c=0#include <iostream>#include <math.h>using namespace std;void mainfloat a,b,c,disc;cout<<"please input a,b,c:"cin>>a>>b>>c;if a=0cerr<<"a is equal to zero,

4、error!"<<endl;elseif disc=b*b-4*a*c<0cerr<<"disc=b*b-4*a*c<0"<<endl;elsecout<<"x1="<<-b+sqrtdisc/2*a<<endl;cout<<"x2="<<-b-sqrtdisc/2*a<<endl;please input a,b,c:0 2 3a is equal to zero,error!please input a

5、,b,c:5 2 3disc=b*b-4*a*c<0please input a,b,c:1 2.5 1.5x1=-1x2=-1.53. clog流對(duì)象clog流也是標(biāo)準(zhǔn)錯(cuò)誤流,也是在顯示器上顯示出錯(cuò)信息。與cerr的微小區(qū)別是:cerr不經(jīng)緩沖區(qū),直接向顯示器上輸出有關(guān)信息,而clog中的信息存放在緩沖區(qū)中,緩沖區(qū)滿后或遇到endl時(shí)向顯示器輸出。13.2.2格式輸出1. 使用控制符控制輸出格式例13.2用控制符控制輸出格式#include <iostream>#include <iomanip>using namespace std;int mainint a

6、; cout<<"input a:" cin>>a; cout<<"dec:"<<dec<<a<<endl; cout<<"hex:"<<hex<<a<<endl; cout<<"oct:"<<setbase8<<a<<endl; char *pt="China" cout<<setw10<<pt<&

7、lt;endl; cout<<setfill'*'<<setw10<<pt<<endl; double pi=22.0/7.0; cout<<setiosflagsios:scientific<<setprecision8; cout<<"pi="<<pi<<endl; cout<<"pi="<<setprecision4<<pi<<endl; cout<<"pi

8、="<<setiosflagsios:fixed<<pi<<endl; return 0;input a:34dec:34hex:22oct:42 China*Chinapi=3.14285714e+000pi=3.1429e+000pi=3.1432. 用流對(duì)象的成員函數(shù)控制輸出格式例13.3 用流控制成員函數(shù)輸出數(shù)據(jù)。#include <iostream>using namespace std;void mainint a=21;cout.setfios:showbase;cout<<"dec:"&l

9、t;<a<<endl;cout.unsetfios:dec;cout.setfios:hex;cout<<"hex:"<<a<<endl;cout.unsetfios:hex;cout.setfios:oct;cout<<"oct:"<<a<<endl;char *pt="China"cout.width10;cout<<pt<<endl;cout.width10;cout.fill'*'cout<&

10、lt;pt<<endl;double pi=22.0/7.0;cout.setfios:scientific;cout<<"pi="cout.width14;cout<<pi<<endl;cout.unsetfios:scientific;cout.setfios:fixed;cout.width12;cout.setfios:showpos;cout.setfios:internal;cout.precision6;cout<<pi<<endl;dec:21hex:0x15oct:025 China*

11、Chinapi=*3.142857e+000+*3.14285713.2.3用流成員函數(shù)put輸出字符put是用于輸出單個(gè)字符的成員函數(shù)。cout.put'a'cout.put65+32;例13.4 有一個(gè)字符串BASIC,要求把它按相反的順序輸出。#include <iostream>using namespace std;int mainchar *a="BASIC" forint i=4;i>=0;i- cout.put*a+i; cout.put'n' return 0;也可以用putchar函數(shù)實(shí)現(xiàn)。#includ

12、e <iostream>int mainchar *a="BASIC" forint i=4;i>=0;i- putchar*a+i; putchar'n' return 0;13.3標(biāo)準(zhǔn)輸入流13.3.1 cin流可以通過測試cin的值,判斷流對(duì)象是否處于正常狀態(tài)和提取操作是否成功。if!cincerr<<"error"例13.5 通過測試cin的值,判斷流對(duì)象是否處于正常狀態(tài)。#include <iostream>using namespace std;void mainfloat grade

13、;cout<<"enter grade:"whilecin>>gradeifgrade>=85 cout<<grade<<" GOOD!"<<endl;ifgrade<60 cout<<grade<<" fail!"<<endl;cout<<"enter grade:"cout<<"The end."<<endl;enter grade:67enter

14、grade:8989 GOOD!enter grade:5656 fail!enter grade:100100 GOOD!enter grade:wThe end.13.3.2用于字符輸入的流成員函數(shù)1. 用get函數(shù)讀入一個(gè)字符(1) 不帶參數(shù)的get函數(shù)cin.get;用來從指定的輸入流中提取一個(gè)字符包括空白字符,函數(shù)返回值就是讀入的字符。假設(shè)遇到文件完畢符CTRL+Z,那么函數(shù)返回文件完畢標(biāo)志EOFEnd Of File,一般以-1代表EOF。例13.6 用get函數(shù)讀入字符。#include <iostream>using namespace std;void mainc

15、har c;cout<<"enter a sentence:"<<endl;whilec=cin.get!=EOFcout.putc;enter a sentence:I study C+ very hard.I study C+ very hard.(2) 有一個(gè)參數(shù)的get函數(shù)cin.getch;其作用是從輸入流中讀取一個(gè)字符,賦給字符變量ch。讀取成功返回真,失敗遇到文件完畢符等返回假。#include <iostream>using namespace std;void mainchar c;cout<<"e

16、nter a sentence:"<<endl;whilecin.getccout.putc; cout<<"end"<<endl;(3) 有三個(gè)參數(shù)的get函數(shù)cin.get字符指針,字符個(gè)數(shù)n,終止字符其作用是從輸入流中讀取n-1個(gè)字符,賦值個(gè)指定的字符指針指向的地方。假設(shè)在讀取n-1個(gè)字符之前遇到指定的終止字符,那么提早完畢讀取。假設(shè)讀取成功那么函數(shù)返回非真,如失敗遇到文件完畢符返回0假。#include <iostream>using namespace std;void mainchar ch20;cout

17、<<"enter a sentence:"<<endl;cin.getch,10,'n'cout<<ch<<endl;enter a sentence:I study C+ very hard.I study Cget函數(shù)的第三個(gè)參數(shù)可以省略,此時(shí)默認(rèn)為n。2. 用成員函數(shù)getline函數(shù)讀入一行字符getline成員函數(shù)的作用是從輸入流中讀取一行字符,其用法與帶三個(gè)參數(shù)的get函數(shù)類似。例13.7 用getline函數(shù)讀入一行字符。#include <iostream>using namespa

18、ce std;void mainchar ch20;cout<<"enter a sentence:"<<endl;cin>>ch;cout<<"The string read with cin is:"<<ch<<endl;cin.getlinech,20,'/'cout<<"The second part is:"<<ch<<endl;cin.getlinech,20;cout<<"Th

19、e third part is:"<<ch<<endl;enter a sentence:I like C+./I study C+./I am happy.The string read with cin is:IThe second part is: like C+.The third part is:I study C+./I am h下一個(gè)getline函數(shù)將從該終止標(biāo)志的下一個(gè)字符開場讀入。13.3.3 istream類的其他成員函數(shù)1. eof 函數(shù)從輸入流中讀取數(shù)據(jù),假設(shè)到達(dá)文件末尾,eof函數(shù)值為真,否那么為假。例13.8 逐個(gè)讀入一行字符,將

20、其中的非空格字符輸出。#include <iostream>using namespace std;void mainchar c;while!cin.eofifc=cin.get!=' 'cout.putc;C+ is very interesting.C+isveryinteresting.2. peek函數(shù)c=cin.peek;函數(shù)的返回值是指針指向的當(dāng)前字符,但它只是觀測,指針仍停留在當(dāng)前位置,并不后移。3. putback函數(shù)cin.putbackch;其作用是將前面用get或getline函數(shù)從輸入流中讀取的字符返回到輸入流,插到當(dāng)前指針位置,以供后面讀

21、取。例13.9 peek函數(shù)和putback函數(shù)的用法。#include <iostream>using namespace std;void mainchar c20;int ch;cout<<"please enter a sentence."<<endl;cin.getlinec,15,'/'cout<<"The first part is:"<<c<<endl;ch=cin.peek;cout<<"The next characterAS

22、CII code is:"<<ch<<endl;cin.putbackc0;cin.getlinec,15,'/'cout<<"The second part is:"<<c<<endl;please enter a sentence.I am a boy./ am a student./The first part is:I am a boy.The next characterASCII code is:32The second part is:I am a student圖13.44

23、. ignore函數(shù)cin.ignoren,終止字符函數(shù)的作用是跳過輸入流中的n個(gè)字符,或遇到指定的終止字符時(shí)提早完畢。ignore5,A;/跳過輸入流中的5個(gè)字符,遇到A后提早完畢。ignore;相當(dāng)于 ignore1,EOF;例13.10 用ignore函數(shù)跳過輸入流中的字符。不用ignore函數(shù)的情況。#include <iostream>using namespace std;void mainchar ch20;cin.getch,20,'/'cout<<"The first part is:"<<ch<&

24、lt;endl;cin.getch,20,'/'cout<<"The second part is:"<<ch<<endl;I like C+./I study C+./I am happy.The first part is:I like C+.The second part is:#include <iostream>using namespace std;void mainchar ch20;cin.getch,20,'/'cout<<"The first part

25、is:"<<ch<<endl;cin.ignore;cin.getch,20,'/'cout<<"The second part is:"<<ch<<endl;I like C+./I study C+./I am happy.The first part is:I like C+.The second part is:I study C+.13.4文件操作與文件流13.4.1文件的概念文件:一般是存儲(chǔ)在外部介質(zhì)硬盤,光盤,U盤上的數(shù)據(jù)的集合。ASCII文件:它的每一個(gè)字節(jié)放一個(gè)ASCII

26、代碼,代表一個(gè)字符,也稱文本文件。用于存儲(chǔ)字符信息。二進(jìn)制文件:是把內(nèi)存中的數(shù)據(jù)按其在內(nèi)存中的存儲(chǔ)形式原樣輸出到磁盤上存放。例如整數(shù)100000圖13.513.4.2文件流類與文件流對(duì)象文件流是以外存文件為輸入輸出對(duì)象的數(shù)據(jù)流。輸出文件流是從內(nèi)存流向外存文件的數(shù)據(jù),輸入文件流是從外存文件流向內(nèi)存的數(shù)據(jù)。每一個(gè)文件流都有一個(gè)內(nèi)存緩沖區(qū)與之對(duì)應(yīng)。用于文件操作的文件流類:ifstream類,是從istream類派生的。用來讀磁盤文件。ofstream類,是從ostream類派生的。用來寫磁盤文件。fstream類,是從iostream類派生的。用來讀、寫磁盤文件。ofstream outfile;/

27、建立一個(gè)輸出文件流對(duì)象。13.4.3文件的翻開與關(guān)閉1. 翻開磁盤文件(1) 調(diào)用文件流成員函數(shù)open。ofstream outfile;/ 建立一個(gè)輸出文件流對(duì)象。outfile.open"f1.dat",ios:out;使輸出文件流outfile與f1.dat文件建立關(guān)聯(lián),此時(shí)f1.dat是一個(gè)輸出文件,用于接收從內(nèi)存輸出的數(shù)據(jù)。調(diào)用成員函數(shù)open的一般格式為:文件流對(duì)象.open磁盤文件名,輸入輸出方式;(2) 在定義文件流時(shí)指定參數(shù)ofstream outfile"f1.dat",ios:out;作用與open函數(shù)一樣。說明:1新版本I/O類

28、庫中不提供ios:nocreate和ios:noreplace。2每一個(gè)翻開的文件都有一個(gè)文件指針,該指針的初始位置由I/O方式?jīng)Q定,每次讀寫都從文件指針的當(dāng)前位置開場。每讀入一個(gè)字節(jié),指針就后移一個(gè)字節(jié)。當(dāng)文件指針移到最后,就會(huì)遇到文件完畢符CTRL+Z,此時(shí)一般返回EOF值為-1。此時(shí)流對(duì)象的成員函數(shù)eof的值為真,表示文件完畢了。3可以用“位或運(yùn)算符“|對(duì)輸出方式進(jìn)展組合。ios:in|ios:nocreate4假設(shè)翻開操作失敗,open函數(shù)的返回值為0假,假設(shè)是調(diào)用構(gòu)造函數(shù)的方式翻開文件的,那么流對(duì)象的值為0??梢該?jù)此判斷翻開是否成功。ifoutfile.open"f1.da

29、t",ios:app=0cout<<"open error"if!outfile.open"f1.dat",ios:appcout<<"open error"2. 關(guān)閉磁盤文件關(guān)閉文件的成員函數(shù)close。outfile.close;所謂的關(guān)閉,就是解除該磁盤文件與文件流對(duì)象的關(guān)聯(lián)。此時(shí),該文件流對(duì)象就可以和其它磁盤文件關(guān)聯(lián)。13.4.4對(duì)ASCII文件的操作對(duì)ASCII文件的讀寫操作可以用以下兩種方法:(1) 用流插入運(yùn)算符“<<和流提取運(yùn)算符“>>輸入輸出標(biāo)準(zhǔn)類型的數(shù)據(jù)。(

30、2) 用流的成員函數(shù)put,get,getline等進(jìn)展輸入和輸出。例13.11 有一個(gè)整型數(shù)組,含10個(gè)元素,從鍵盤輸入10個(gè)整數(shù)給數(shù)組,將此數(shù)組送到磁盤文件中存放。#include <iostream>#include <fstream>using namespace std;void mainint a10;ofstream outfile"f1.dat"/ofstream outfile"f1.dat",ios:out;if!outfilecerr<<"open error!"<<

31、;endl;exit1;cout<<"enter 10 integer numbers:"<<endl;forint i=0;i<10;i+cin>>ai;outfile<<ai<<" "outfile.close;enter 10 integer numbers:1 3 5 2 4 6 10 8 7 9例13.12 從例13.11建立的數(shù)據(jù)文件f1.dat中讀入10個(gè)整數(shù)放在數(shù)組中,找出并輸出10個(gè)數(shù)中的最大者和它在數(shù)組中的序號(hào)。#include <iostream>#inc

32、lude <fstream>using namespace std;void mainint a10,max,i,order;ifstream infile"f1.dat",ios:in; if!infilecerr<<"open error!"<<endl;exit1;fori=0;i<10;i+infile>>ai;cout<<ai<<" "cout<<endl;max=a0;order=0;fori=1;i<10;i+ifai>

33、maxmax=ai;order=i;cout<<"max="<<max<<endl<<"order="<<order<<endl;infile.close;1 3 5 2 4 6 10 8 7 9max=10order=6例13.13 從鍵盤讀入一行字符,把其中的字母字符依次存放在磁盤文件f2.dat中。再把它從磁盤文件讀入程序,將其中的小寫字母改寫為大寫字母,再存入磁盤文件f3.dat。#include <iostream>#include <fstream>

34、;using namespace std;void save_to_fileofstream outfile"f2.dat"if!outfilecerr<<"open f2.dat error!"<<endl;exit1;char c80;cin.getlinec,80;forint i=0;ci;i+ifci>=65 && ci<=90|ci>=97 && ci<=122outfile.putci;cout<<ci;cout<<endl;outfil

35、e.close;void get_from_filechar ch;ifstream infile"f2.dat",ios:in;if!infilecerr<<"open f2.dat error!"<<endl;exit1;ofstream outfile"f3.dat"if!outfilecerr<<"open f3.dat error!"<<endl;exit1;whileinfile.getchifch>=97 && ch<=122

36、ch=ch-32;outfile.putch;cout<<ch;cout<<endl;infile.close;outfile.close;void mainsave_to_file;get_from_file;New Beijing,Great Olympic,2020,China.NewBeijingGreatOlympicChinaNEWBEIJINGGREATOLYMPICCHINA#include <iostream>#include <fstream>using namespace std;void display_filechar

37、*filenameifstream infilefilename,ios:in;if!infilecerr<<"open error!"<<endl;exit1;char ch;whileinfile.getchcout.putch;cout<<endl;infile.close;void maindisplay_file"f3.dat"NEWBEIJINGGREATOLYMPICCHINA13.4.5對(duì)二進(jìn)制文件的操作1. 用成員函數(shù)read和write讀寫二進(jìn)制文件istream& readchar *bu

38、ffer,int len;ostream& writeconst char *buffer,int len;例13.14將一批數(shù)據(jù)以二進(jìn)制形式存放在磁盤文件中。#include <iostream>#include <fstream>using namespace std;struct studentchar name20;int num;int age;char sex;void mainstudent stud3="Li",1001,18,'f',"Fun",1002,19,'m',&q

39、uot;Wang",1004,17,'f'ofstream outfile"stud.dat",ios:binary;if!outfilecerr<<"open error!"<<endl;abort;forint i=0;i<3;i+ outfile.writechar*&studi,sizeofstudi;/ outfile.writechar *&stud0,sizeofstud;/可以替代上述for,一條語句完成寫工作。outfile.close;例13.15 將剛剛以二進(jìn)制

40、形式存放在磁盤文件中的數(shù)據(jù)讀入內(nèi)存并在顯示器上顯示。#include <iostream>#include <fstream>using namespace std;struct studentchar name20;int num;int age;char sex;void mainstudent stud3;int i;ifstream infile"stud.dat",ios:binary;if!infilecerr<<"open error!"<<endl;abort;fori=0;i<3;i

41、+infile.readchar*&studi,sizeofstudi;infile.close;fori=0;i<3;i+cout<<"NO."<<i+1<<endl;cout<<"name:"<<<<endl;cout<<"num:"<<studi.num<<endl;cout<<"age:"<<studi.age<<endl;cout

42、<<"sex:"<<studi.sex<<endl<<endl;NO.1name:Linum:1001age:18sex:fNO.2name:Funnum:1002age:19sex:mNO.3name:Wangnum:1004age:17sex:f2. 與文件指針有關(guān)的流成員函數(shù)函數(shù)參數(shù)中的“文件中的位置和“位移量已被指定為long型整數(shù),以字節(jié)為單位。ios:beg 文件開頭ios:cur 指針當(dāng)前位置ios:end 文件末尾infile.seekg100;infile.seekg-50,ios:cur;outfile.s

43、eekp-75,ios:end;2. 隨機(jī)訪問二進(jìn)制數(shù)據(jù)文件例13.6 有5個(gè)學(xué)生的數(shù)據(jù),要求:(1) 把它們存到磁盤文件中;(2) 將磁盤文件中的第1、3、5個(gè)學(xué)生數(shù)據(jù)讀入程序,并顯示出來(3) 將第3個(gè)學(xué)生的數(shù)據(jù)修改后存回磁盤文件中的原有位置。(4) 從磁盤文件讀入修改后的5個(gè)學(xué)生的數(shù)據(jù)并顯示出來。#include <iostream>#include <fstream>using namespace std;struct studentint num;char name20;float score;void mainint i; student stud5=100

44、1,"Li",85,1002,"Fun",97.5, 1004,"Wang",54,1006,"Tan",76.5,1010,"ling",96; fstream iofile"stud.dat",ios:in|ios:out|ios:binary; if!iofile cerr<<"open error!"<<endl;abort; fori=0;i<5;i+ iofile.writechar *&studi,siz

45、eofstudi; student stud15; fori=0;i<5;i=i+2 iofile.seekgi*sizeofstudi,ios:beg; iofile.readchar *&stud1i/2,sizeofstud1i; cout<<stud1i/2.num<<" "<<stud1i/2.name<<" "<< stud1i/2.score<<endl; cout<<endl; stud2.num=1012;

46、,"Wu" stud2.score=60; iofile.seekp2*sizeofstud0,ios:beg; iofile.writechar *&stud2,sizeofstud2; iofile.seekg0,ios:beg; fori=0;i<5;i+ iofile.readchar *&studi,sizeofstudi; cout<<studi.num<<" "<<<<" " <<studi.score<<e

47、ndl; iofile.close;1001 Li 851004 Wang 541010 ling 961001 Li 851002 Fun 97.51012 Wu 601006 Tan 76.51010 ling 9613.5字符串流字符串流是以內(nèi)存中用戶定義的字符數(shù)組為輸入輸出對(duì)象。1. 建立輸出字符串流對(duì)象ostrstream:ostrstreamchar *buffer,int n,int mode=ios:out;ostrstream stroutch,20;建立輸出字符串流對(duì)象strout,并使strout與字符數(shù)組ch關(guān)聯(lián),流緩沖區(qū)大小20。2. 建立輸入字符串流對(duì)象istrst

48、ream:istrstreamchar *buffer;istrstream:istrstreamchar *buffer,int n;istrstream strinch2;建立輸入字符串流對(duì)象strin,將字符數(shù)組ch2中的全部數(shù)據(jù)作為輸入字符串流的內(nèi)容。istrstream strinch2,20;將ch2中的前20個(gè)字符作為輸入字符串流的內(nèi)容。3. 建立輸入輸出字符串流對(duì)象 strstream:strstreamchar *buffer,int n,int mode; strstream strioch3,sizeofch3,ios:in|ios:out;例13.17 將一組數(shù)據(jù)保存在

49、字符數(shù)組中。#include <strstream>#include <iostream>using namespace std;struct studentint num;char name20;float score;void mainstudent stud3=1001,"Li",78, 1002,"Wang",89.5,1004,"Fun",90;char c50;ostrstream stroutc,30; / ostrstream stroutc,10;forint i=0;i<3;i+ st

50、rout<<studi.num<<<<studi.score;strout<<ends;cout<<"array c:"<<endl<<c<<endl;array c:1001Li781002Wang89.51004Fun90例13.18 在一個(gè)字符數(shù)組c中存放了10個(gè)整數(shù),以空格相間隔,要求將它們放到整型數(shù)組中,再按大小排序,然后在存放回字符數(shù)組c中。#include <strstream>#include <iostream>usi

51、ng namespace std;void mainchar c50="12 34 65 -23 -32 33 61 99 321 32"int a10,i,j,t;cout<<"array c:"<<c<<endl;istrstream strinc,sizeofc;fori=0;i<10;i+strin>>ai;cout<<"array a:"fori=0;i<10;i+cout<<ai<<" "cout<&l

52、t;endl;fori=0;i<9;i+forj=0;j<9-i;j+ifaj>aj+1t=aj;aj=aj+1;aj+1=t;ostrstream stroutc,sizeofc;fori=0;i<10;i+strout<<ai<<" "strout<<ends;cout<<"array c:"<<c<<endl;array c:12 34 65 -23 -32 33 61 99 321 32array a:12 34 65 -23 -32 33 61 99

53、 321 32array c:-32 -23 12 32 33 34 61 65 99 321實(shí)驗(yàn):1:實(shí)驗(yàn)?zāi)康模?深化理解C+的輸入輸出的含義與其實(shí)現(xiàn)方法。2掌握標(biāo)準(zhǔn)輸入輸出流的應(yīng)用,包括格式輸入輸出。3掌握對(duì)文件的輸入和輸出操作。2:實(shí)驗(yàn)內(nèi)容: P458 1 43:實(shí)驗(yàn)結(jié)果。7.4.4 創(chuàng)立文件流操作系統(tǒng)中另一個(gè)常配置的設(shè)備是文件設(shè)備磁盤、磁帶等,在文件設(shè)備上可建立一個(gè)文件,對(duì)文件進(jìn)展操作,在文件處理完之后還應(yīng)關(guān)閉文件。系統(tǒng)通過對(duì)流類進(jìn)一步擴(kuò)展,提供了支持文件的才能,這使得程序員在建立和使用文件時(shí),就像使用cin和cout一樣方便。圖7.2表示擴(kuò)展處理文件的類型級(jí),新派生的五個(gè)類用于文件處

54、理。filebuf類是streambuf的派生用類,它提供了對(duì)文件緩沖區(qū)管理的才能,我們一般不涉及這個(gè)類。fstreambase類提供了文件處理所需要的全部成員函數(shù),在它的派生類中沒有提供的新的成員函數(shù);ifstream類用于文件的輸入操作;而ofstream類用于文件的輸出操作;fstream類允許對(duì)文件在兩個(gè)方向上進(jìn)展操作,這幾個(gè)類也同時(shí)繼承了我們前面介紹的流類的根本類等級(jí)中定義的成員函數(shù)。在使用類時(shí),必須在程序中嵌入文件fstream.h 。1. 文件的翻開和關(guān)閉通過翻開一個(gè)文件,可以將一個(gè)流與一個(gè)文件相聯(lián)結(jié)。一個(gè)文件在翻開時(shí),必須指定文件翻開方式。在類中定義的一組枚舉常量名給出了可允許

55、的文件翻開方式如下所示: ios istreamifstream iostream ostream filebufstreambuf fstreamfstreambaseofstream一個(gè)指針成員指向該類對(duì)象 圖7.2 文件I/O流庫類等級(jí)常量名 含 義in 翻開文件進(jìn)展讀操作out 翻開文件進(jìn)展寫操作ate 文件翻開時(shí)將文件指針定位于文件尾app 把要輸出給文件的內(nèi)容都添加到文件尾trunc 假設(shè)文件存在,將其長度截?cái)酁榱悴⑷コ袃?nèi)容 nocreate 假設(shè)文件不存在,那么翻開操作失敗noreplace 假設(shè)文件存在,除非設(shè)置ios : ate或ios : app,否那么翻開文件失敗bi

56、nary 指定文件以二進(jìn)制方式翻開,缺省為文本方式ios:app The function performs a seek to the end of file. When new bytes are written to the file, they are always appended to the end, even if the position is moved with the ostream:seekp function.ios:ate The function performs a seek to the end of file. When the first new byt

57、e is written to the file, it is appended to the end, but when subsequent bytes are written, they are written to the current position除ios : app方式之外,文件剛翻開時(shí),指示當(dāng)前讀寫位置的文件指針定位于文件的開場位置,而ios :app使文件當(dāng)前的寫指針定位于文件尾。在翻開一個(gè)文件時(shí),假設(shè)指定方式ios : out,而未指定ios : ate 或ios : app,那么隱含方式為ios :trunc。也可以將幾種方式通過“或操作結(jié)合起來,例如翻開一個(gè)供讀寫的

58、文件,其方式可以定義為ios : in|ios : out??梢杂袃煞N方法以一定方式翻開一個(gè)文件 ,一種方式是在建立時(shí)使用構(gòu)造函數(shù)將一個(gè)文件和這個(gè)流對(duì)象聯(lián)結(jié)起來。在ifstream類、ofstream類和fstream類中定義有一個(gè)構(gòu)造函數(shù),它們是:ifstream : ifstreamchar * ,int=ios : in,int =filebuf : openprot;ofstream : ofstreamchar * ,int=ios : out,int=flebuf : openprot;fstream :; fstreamchar * ,int,int=filebuf : oper

59、prot;在這三個(gè)構(gòu)造函數(shù)中,第一參數(shù)表示要聯(lián)結(jié)的文件名,第二個(gè)參數(shù)指定該文件翻開的方式,ifstream類和ofstream類的構(gòu)造函數(shù)提供了缺省值。假設(shè)不使用這個(gè)缺省值時(shí),那么必須在指定的方式中含有ios : in或ios : out。最后一個(gè)參數(shù)指定文件的保護(hù)方式,這個(gè)值和詳細(xì)的操作系統(tǒng)有關(guān),我們一般只使用缺省指定的filebuf :openprot值,它是在filebuf 類中定義的一個(gè)公有的靜態(tài)數(shù)據(jù)成員。【例7.8】將一個(gè)文件的內(nèi)容拷貝到另一個(gè)文件中。#include<fstream.h>#include<string.h>#include<stdlib

60、.h>void mainifstream input"c:xx.cpp"if!inputcout<<"Can't open file;"<<"c:xx.cpp"<<endl;exit1;ofstream output"c:yy.cpp"if!output cout<<"Can't open file;"<<"c:yy.cpp"<<endl;exit1;/input.tie&output;char ch;/input.unsetfios:skipws;whileinput>>ch/

溫馨提示

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

評(píng)論

0/150

提交評(píng)論