




已閱讀5頁,還剩13頁未讀, 繼續(xù)免費閱讀
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
C+程序設(shè)計(第3版)第13章輸入輸出流13.1C+的輸入和輸出13.1.1輸入輸出的含義13.1.2C+的I/O對C的發(fā)展類型安全和可擴展性13.1.3C+的輸入輸出流1. C+的流庫圖13.1圖13.2圖13.32. 與流類庫有關(guān)的頭文件3. 在iostream頭文件中定義的流對象13.2標(biāo)準(zhǔn)輸出流13.2.1cout,cerr和clog流編寫程序: #include #include using namespace std;int main() float a,b,c,disc;coutabc; if (a=0)cerra is equal to zero,error!endl;/將有關(guān)出錯信息插入cerr流,在屏幕輸出elseif (disc=bb-4ac)0)cerrdisc=bb-4ac0endl; /將有關(guān)出錯信息插入cerr流,在屏幕輸出else coutx1=(-b+sqrt(disc)/(2a)endl;coutx2=(-b-sqrt(disc)/(2a)endl;return 0;運行結(jié)果: please input a,b,c:0 2 3a is equal to zero,error! please input a,b,c:5 2 3disc=bb-4ac0 please input a,b,c:12.51.5x1=-1x2=-1.51. cout流對象2. cerr流對象3. clog流對象13.2.2標(biāo)準(zhǔn)類型數(shù)據(jù)的格式輸出1. 使用控制符控制輸出格式編寫程序:#include using namespace std;int main() int a=21cout.setf(iosshowbase);/顯示基數(shù)符號(0x或0)coutdec:aendl; /默認以十進制形式輸出acout.unsetf(iosdec); /終止十進制的格式設(shè)置cout.setf(ioshex); /設(shè)置以十六進制輸出的狀態(tài)Bcouthex:aendl; /以十六進制形式輸出acout.unsetf(ioshex); /終止十六進制的格式設(shè)置cout.setf(iosoct); /設(shè)置以八進制輸出的狀態(tài)coutoct:aendl; /以八進制形式輸出a char pt=China;/pt指向字符串Chinacout.width(10);/指定域?qū)挒?0coutptendl;/輸出字符串cout.width(10);/指定域?qū)挒?0cout.fill();/指定空白處以填充coutptendl;/輸出字符串double pi=22.0/7.0;/輸出pi值cout.setf(iosscientific);/指定用科學(xué)記數(shù)法輸出coutpi=; /輸出pi=cout.width(14);/指定域?qū)挒?4coutpiendl;/輸出pi值cout.unsetf(iosscientific); /終止科學(xué)記數(shù)法狀態(tài)cout.setf(iosfixed);/指定用定點形式輸出cout.width(12); /指定域?qū)挒?2cout.setf(iosshowpos);/正數(shù)輸出+號cout.setf(iosinternal); /數(shù)符出現(xiàn)在左側(cè)cout.precision(6);/保留6位小數(shù)coutpiendl; /輸出pi,注意數(shù)符+的位置return 0;運行結(jié)果: dec:21(十進制形式)hex:0x15 (十六進制形式,以0x開頭)oct:025(八進制形式,以0開頭) China (域?qū)挒?0) China (域?qū)挒?0,空白處以填充)pi= 3.142857e+00(指數(shù)形式輸出,域?qū)?4,默認6位小數(shù),空白處以填充)+ 3.142857 (小數(shù)形式輸出,精度為6,最左側(cè)輸出數(shù)符+)說明: (1) 成員函數(shù)width(n)和控制符setw(n)只對其后的第1個輸出項有效。如cout.width(6);cout203.14endl;輸出結(jié)果為203.1413.2.3用流成員函數(shù)put輸出字符編寫程序: #include using namespace std;int main() char p=BASIC;/字符指針指向Bfor(int i=4;i=0;i-)cout.put(p+i);/從最后一個字符開始輸出cout.put(n);return 0;運行結(jié)果: CISAB編寫程序:#include /也可以用#include ,同時不要下一行using namespace std;int main() char p=BASIC;for(int i=4;i=0;i-)putchar(p+i);putchar(n);運行結(jié)果:與前相同。13.3標(biāo)準(zhǔn)輸入流13.3.1cin流13.3.2用于字符輸入的流成員函數(shù)1. 用get函數(shù)讀入一個字符編寫程序:#include int main() int c;coutenter a sentence:endl;while(c=cin.get()!=EOF)cout.put(c);return 0;從鍵盤輸入一行字符,用cin.get()逐個讀入字符,將讀入字符賦給字符變量c。如果c的值不等于EOF,表示已成功地讀入一個有效字符,然后通過put函數(shù)輸出該字符。運行結(jié)果: enter a sentence:I study C+very hard.(輸入一行字符)I study C+very hard.(輸出該行字符)Z(程序結(jié)束)2. 用成員函數(shù)getline函數(shù)讀入一行字符13.3.3istream類的其他成員函數(shù)編寫程序: #include using namespace std;int main() char c;while(!cin.eof()/eof()為假表示未遇到文件結(jié)束符if(c=cin.get()!= )/檢查讀入的字符是否空格字符cout.put(c);return 0;運行結(jié)果: C+is very interesting.C+isveryinteresting.Z(結(jié)束)2. peek函數(shù)peek是“觀察”的意思,peek函數(shù)的作用是觀測下一個字符。其調(diào)用形式為c=cin.peek();1. eof函數(shù)2. peek函數(shù)3. putback函數(shù)編寫程序:#include using namespace std;int main() char c20;int ch;coutplease enter a sentence:endl;cin.getline(c,15,/);coutThe first part is:cendl;ch=cin.peek();/觀看當(dāng)前字符coutThe next character(ASCII code) is:chendl;cin.putback(c0); /將I插入到指針?biāo)柑?cin.getline(c,15,/);coutThe second part is:cendl;return 0;運行結(jié)果: please enter a sentence: I am a boy./ am a student./The first part is:I am a boy.The next character(ASCII code) is:32(下一個字符是空格)The second part is:I am a student.圖13.44. ignore函數(shù)編寫程序:先看不用ignore函數(shù)的情況: #include using namespace std;int main()char ch20;cin.get(ch,20,/);coutThe first part is:chendl;cin.get(ch,20,/);coutThe second part is:chendl;return 0;運行結(jié)果: I like C+./I study C+./I am happy.The first part is:I like C+.The second part is: (字符數(shù)組ch中沒有從輸入流中讀取有效字符)13.4對數(shù)據(jù)文件的操作與文件流13.4.1文件的概念圖13.513.4.2文件流類與文件流對象13.4.3文件的打開與關(guān)閉1. 打開磁盤文件13.4.4對ASCII文件的操作編寫程序:#include using namespace std;int main() int a10;ofstream outfile(f1.dat,iosout);/定義文件流對象,打開磁盤文件f1.datif(!outfile)/如果打開失敗,outfile返回0值 cerropen error!endl;exit(1);coutenter 10 integer numbers:endl;for(int i=0;iai;outfileai ;/向磁盤文件f1.dat輸出數(shù)據(jù)outfile.close(); /關(guān)閉磁盤文件f1.datreturn 0;運行結(jié)果: enter 10 integer numbers:1 3 5 2 4 6 10 8 7 9 編寫程序: #include using namespace std;int main() int a10,max,i,order;ifstream infile(f1.dat,iosin|iosnocreate);/定義輸入文件流對象,以輸入方式打開磁盤文件f1.datif(!infile) cerropen error!endl;exit(1);for(i=0;iai;/從磁盤文件讀入10個整數(shù),順序存放在a數(shù)組中coutai ;/在顯示器上順序顯示10個數(shù)coutendl;max=a0;order=0;for(i=1;imax) max=ai;/將當(dāng)前最大值放在max中order=i; /將當(dāng)前最大值的元素序號放在order中coutmax=maxendlorder=orderendl;infile.close();return 0;運行結(jié)果: 1 3 5 2 4 6 10 8 7 9(在磁盤文件中存放的10個數(shù))max=10 (最大值為10)order=6(最大值是數(shù)組中序號為6的元素)編寫程序: #include using namespace std;/ save_to_file函數(shù)從鍵盤讀入一行字符并將其中的字母存入磁盤文件void save_to_file() ofstream outfile(f2.dat); /定義輸出文件流對象outfile,以輸出方式打開磁盤文件f2.datif(!outfile) cerropen f2.dat error!=65 & ci=97 & ci=122)/如果是字母字符 outfile.put(ci);/將字母字符存入磁盤文件f2.datcoutci;/同時送顯示器顯示coutendl;outfile.close(); /關(guān)閉f2.dat/從磁盤文件f2.dat讀入字母字符,將其中的小寫字母改為大寫字母,再存入f3.datvoid get_from_file() char ch;ifstream infile(f2.dat,iosin|iosnocreate); /定義輸入文件流outfile,以輸入方式打開磁盤文件f2.dat if(!infile) cerropen f2.dat error!endl;exit(1);ofstream outfile(f3.dat);/定義輸出文件流outfile,以輸出方式打開磁盤文件f3.dat if(!outfile) cerropen f3.dat error!=97 & ch=122)/判斷ch是否為小寫字母ch=ch-32;/將小寫字母變?yōu)榇髮懽帜竜utfile.put(ch); /將該大寫字母存入磁盤文件f3.datcoutch;/同時在顯示器輸出coutendl;infile.close();/關(guān)閉磁盤文件f2.datoutfile.close(); /關(guān)閉磁盤文件f2.dat int main() save_to_file(); /調(diào)用save_to_file(),從鍵盤讀入一行字符并將其中的字母存入磁盤文件f2.datget_from_file(); /調(diào)用get_from_file(),從f2.dat讀入字母字符,改為大寫字母,再存入f3.dat return 0;運行結(jié)果: New Beijing, Great Olypic, 2008, China.NewBeijingGreatOlypicChina (將字母寫入磁盤文件f2.dat,同時在屏幕顯示)NEWBEIJINGGREATOLYPICCHINA (改為大寫字母)13.4.5對二進制文件的操作編寫程序: #include using namespace std; struct student char name20; int num;int age;char sex;int main()student stud3=Li,1001,18,f,Fang,1002,19,m,Wang,1004,17,f;ofstream outfile(stud.dat,iosbinary); if(!outfile) cerropen error!endl; abort();/退出程序 for(int i=0;i3;i+) outfile.write(char)&studi,sizeof(studi); outfile.close(); return 0;編寫程序:#include using namespace std;struct student string name;int num;int age;char sex;int main() student stud3;int i;ifstream infile(stud.dat,iosbinary);if(!infile) cerropen error!endl;abort();for(i=0;i3;i+)infile.read(char)&studi,sizeof(studi);infile.close();for(i=0;i3;i+) coutNO.i+1endl;coutname:endl;coutnum:studi.numendl;coutage:studi.ageendl;coutsex:studi.sexendlendl;return 0;運行結(jié)果: NO.1name: Li num: 1001age: 18sex: fNO.2name: Fang num: 1001age: 19sex: mNO.3name: Wangnum: 1004age: 17sex: f1. 用成員函數(shù)read和write讀寫二進制文件2. 與文件指針有關(guān)的流成員函數(shù)編寫程序: #include using namespace std;struct student int num;char name20;float score;int main() student stud5=1001,Li,85,1002,Fang,97.5,1004,Wang,54,1006,Tan,76.5,1010,ling,96;fstream iofile(stud.dat,iosin|iosout|iosbinary);/用fstream類定義輸入輸出二進制文件流對象iofileif(!iofile) cerropen error!endl;abort();for(int i=0;i5;i+)/向磁盤文件輸出5個學(xué)生的數(shù)據(jù)iofile.write(char )&studi,sizeof(studi);student stud15;/用來存放從磁盤文件讀入的數(shù)據(jù)for(int i=0;i5;i=i+2) iofile.seekg(isizeof(studi),iosbeg);/定位于第0,2,4學(xué)生數(shù)據(jù)開頭iofile.read(char )&stud1i/2,sizeof(stud10); /先后讀入3個學(xué)生的數(shù)據(jù),存放在stud10,stud1和stud2中coutstud1i/2.num stud1i/2.name stud1i/2.scoreendl;/輸出stud10,stud1和stud2各成員的值coutendl;stud2.num=1012;/修改第3個學(xué)生(序號為2)的數(shù)據(jù)strcpy(,Wu);stud2.score=60;iofile.seekp(2sizeof(stud0),iosbeg); /定位于第3個學(xué)生數(shù)據(jù)的開頭iofile.write(char )&stud2,sizeof(stud2);/更新第3個學(xué)生數(shù)據(jù)iofile.seekg(0,iosbeg); /重新定位于文件開頭for(int i=0;i5;i+) iofile.read(char )&studi,sizeof(studi);/讀入5個學(xué)生的數(shù)據(jù)coutstudi.num studi.scoreendl;iofile.close();return 0;運行結(jié)果: 1001 Li 85(第1個學(xué)生數(shù)據(jù))1004 Wang 54(第3個學(xué)生數(shù)據(jù))1010 ling 96(第5個學(xué)生數(shù)據(jù))1001 Li 85(輸出修改后5個學(xué)生數(shù)據(jù))1002 Fang 97.51012 Wu 60(已修改的第3個學(xué)生數(shù)據(jù))1006 Tan 76.51010 ling 963. 隨機訪問二進制數(shù)據(jù)文件編寫程序: #include using namespace std; struct student int num;char name20;float score;int main() student stud3=1001,Li,78,1002,Wang,89.5,1004,Fang,90;char c50;/用戶定義的字符數(shù)組ostrstream strout(c,30); /建立輸出字符串流,與數(shù)組c建立關(guān)聯(lián),緩沖區(qū)長30for(int i=0;i3;i+) /向字符數(shù)組c寫3個學(xué)生的數(shù)據(jù)studi.score;stroutends; /ends是C+的I/O操作符,插入一個0coutarray c:cendl;/顯示字符數(shù)組c中的字符運行結(jié)果: array c:1001Li781002Wang89.51004Fang9013.5字符串流1. 建立輸出字符串流對象2. 建立輸入字符串流對象3. 建立輸入輸出字符串流對象編寫程序:#include using namespace std;int main()char c50=12 34 65 -23 -32 33 61 99 321 32;int a10,i,j,t;coutarray c:cendl;/顯示字符數(shù)組中的字符
溫馨提示
- 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)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 美年大健康客服體系優(yōu)化與服務(wù)提升
- 造口護理步驟流程
- 學(xué)前教育五大領(lǐng)域?qū)嵺`
- 肛周膿腫與肛瘺超聲診斷
- 治具設(shè)計培訓(xùn)體系構(gòu)建
- 運動健康小常識
- 硬膜下出血護理
- 健康促進學(xué)校創(chuàng)建工作匯報
- 2025年抗毒素類生物制品項目提案報告
- 書法教學(xué)匯報課件
- 中外航海文化知到課后答案智慧樹章節(jié)測試答案2025年春中國人民解放軍海軍大連艦艇學(xué)院
- 人工智能引論智慧樹知到課后章節(jié)答案2023年下浙江大學(xué)
- 個人競聘報告ppt范文
- GB/T 34300-2017城鄉(xiāng)社區(qū)網(wǎng)格化服務(wù)管理規(guī)范
- GB/T 28267.1-2012鋼絲繩芯輸送帶第1部分:普通用途輸送帶的設(shè)計、尺寸和機械要求
- GB/T 12334-2001金屬和其他非有機覆蓋層關(guān)于厚度測量的定義和一般規(guī)則
- DB22-T 5040-2020建設(shè)工程見證取樣檢測標(biāo)準(zhǔn)-(高清正版)
- 慶七一知識競賽題庫
- 婦幼相關(guān)公共衛(wèi)生服務(wù)督導(dǎo)評估表
- 省級電子政務(wù)外網(wǎng)-統(tǒng)一云平臺建設(shè)方案
- 柯南偵探原理詳解之3柯南與干冰(專業(yè)應(yīng)用)
評論
0/150
提交評論