C與面向?qū)ο蟮某绦蛟O(shè)計(jì) - 程序員聯(lián)合開發(fā)網(wǎng)_第1頁(yè)
C與面向?qū)ο蟮某绦蛟O(shè)計(jì) - 程序員聯(lián)合開發(fā)網(wǎng)_第2頁(yè)
C與面向?qū)ο蟮某绦蛟O(shè)計(jì) - 程序員聯(lián)合開發(fā)網(wǎng)_第3頁(yè)
C與面向?qū)ο蟮某绦蛟O(shè)計(jì) - 程序員聯(lián)合開發(fā)網(wǎng)_第4頁(yè)
C與面向?qū)ο蟮某绦蛟O(shè)計(jì) - 程序員聯(lián)合開發(fā)網(wǎng)_第5頁(yè)
已閱讀5頁(yè),還剩53頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

1、I/O流的概念I(lǐng)/O流的概念I(lǐng)/O流的概念I(lǐng)/O流的概念輸出流輸出流輸出流輸出流輸出流輸出流輸出流輸出流輸出流輸出流ios:skipws Skip white space on input.ios:left Left-align values; pad on the right with the fill character.ios:right Right-align values; pad on the left with the fill character (default alignment).ios:internal Add fill characters after any lea

2、ding sign or base indication, but before the value.ios:dec Format numeric values as base 10 (decimal) (default radix).ios:oct Format numeric values as base 8 (octal).ios:hex Format numeric values as base 16 (hexadecimal).ios:showbase Display numeric constants in a format that can be read by the C+ c

3、ompiler.ios:showpoint Show decimal point and trailing zeros for floating-point values.ios:uppercase Display uppercase A through F for hexadecimal values and E for scientific values.ios:showpos Show plus signs (+) for positive values.ios:scientific Display floating-point numbers in scientific format.

4、ios:fixed Display floating-point numbers in fixed format.ios:unitbuf Cause ostream:osfx to flush the stream after each insertion. By default, cerr is unit buffered.ios:stdio Cause ostream:osfx to flush stdout and stderr after each insertion. 例例 設(shè)置對(duì)齊方式設(shè)置對(duì)齊方式#include#includevoid main( ) double values=

5、1.23, 35.36,653.7,4358.24; char*names=Zoot, Jimmy, Al, Stan; for(int i=0;i4;i+) coutsetiosflags(ios:left)/左對(duì)齊左對(duì)齊 setw(6)namesi resetiosflags(ios:left)/恢復(fù)默認(rèn)恢復(fù)默認(rèn) setw(10)valuesIendl;控制輸出精度:用setprecision操縱符#include#includevoid main( ) double values=1.23,35.36,653.7,4358.24; char*names=Zoot, Jimmy, Al,

6、Stan; for(int i=0;i4;i+) coutsetiosflags(ios:left) setw(6)namesi resetiosflags(ios:left) setw(10)setprecision(1)valuesIendl;輸出流程序運(yùn)行結(jié)果:Zoot 1Jimmy 4e+001Al 7e+002Stan 4e+003若改為若改為setprecision(3),即即3位有效數(shù)字,結(jié)果位有效數(shù)字,結(jié)果成為:成為:Zoot 1.23Jimmy 35.4Al 654Stan 4.36e+003原數(shù)據(jù)為: 1.23, 35.36, 653.7, 4358.24不必使用不必使用r

7、esetprecision,可直接修改精度。,可直接修改精度。輸出流程序運(yùn)行結(jié)果:234 eaea 352352 234程序運(yùn)行結(jié)果:20H 21H ! 22H 23H # 24H $ 25H % 26H & 27H 28H ( 29H ) 2aH * 2bH + 2cH , 2dH - 2eH . 2fH /30H 0 31H 1 32H 2 33H 3 34H 4 35H 5 36H 6 37H 738H 8 39H 9 3aH : 3bH ; 3cH 3fH ?40H 41H A 42H B 43H C 44H D 45H E 46H F 47H G48H H 49H I 4aH

8、 J 4bH K 4cH L 4dH M 4eH N 4fH O50H P 51H Q 52H R 53H S 54H T 55H U 56H V 57H W58H X 59H Y 5aH Z 5bH 5cH 5dH 5eH 5fH _60H 61H a 62H b 63H c 64H d 65H e 66H f 67H g68H h 69H i 6aH j 6bH k 6cH l 6dH m 6eH n 6fH o70H p 71H q 72H r 73H s 74H t 75H u 76H v 77H w78H x 79H y 7aH z 7bH 7cH | 7dH 7eH 輸出流輸出流輸

9、出流輸出流程序運(yùn)行結(jié)果:文件date.dat的內(nèi)容是:06 00 00 00 0A 00 00 00-5C 00 00 00正好是十進(jìn)制數(shù)正好是十進(jìn)制數(shù)6、10、92的二進(jìn)制表示,每個(gè)數(shù)的二進(jìn)制表示,每個(gè)數(shù)4個(gè)字節(jié)。個(gè)字節(jié)。如果以文本方式打開,程序運(yùn)行結(jié)果:文件date.dat的內(nèi)容是:06 00 00 00 0D 0A 00 00-00 5C 00 00 00十進(jìn)制數(shù)十進(jìn)制數(shù)10前面增加了一個(gè)前面增加了一個(gè)0D,即換行符的即換行符的ASCII碼,其余不變。可見并不能直接按文本方式輸出。碼,其余不變。可見并不能直接按文本方式輸出。而且使而且使10的輸出(認(rèn)為是的輸出(認(rèn)為是ASCII碼碼0A)

10、變?yōu)榛剀嚀Q變?yōu)榛剀嚀Q行符。行符。輸出流輸出流輸出流輸入流輸入流輸入流#includevoid main( ) char ch; while (ch=cin.get()!=EOF) cout.put(ch);運(yùn)行時(shí)輸入:運(yùn)行時(shí)輸入: abc xyz 123則輸出:則輸出: abc xyz 123注:先輸入到一個(gè)緩沖區(qū),再由注:先輸入到一個(gè)緩沖區(qū),再由get讀入,讀入,put輸出。輸出。輸入流#includevoid main( ) char ch; while (ch=cin.get()!=EOF) cout.put(ch);運(yùn)行時(shí)輸入:運(yùn)行時(shí)輸入: abc xyz 123則輸出:則輸出: ab

11、c xyz 123注:先輸入到一個(gè)緩沖區(qū),再由注:先輸入到一個(gè)緩沖區(qū),再由get讀入,讀入,put輸出。輸出。例例 讀入一串字符到讀入一串字符到line數(shù)組,以數(shù)組,以t終止終止#include void main() char line100; cout“Type a line terminated by t” endl; cin.getline(line,100,t); coutline;例例 從一個(gè)從一個(gè)payroll文件讀一個(gè)二進(jìn)制記錄到一個(gè)結(jié)構(gòu)中文件讀一個(gè)二進(jìn)制記錄到一個(gè)結(jié)構(gòu)中#include#include#includevoid main( ) struct double sal

12、ary; char name23; employee;ifstream is (payroll,ios:binary |ios:nocreate); if(is) is.read(char*)&employee,sizeof(employee); employee.salaryendl; else coutERROR:Cannot open file payroll.endl; 輸入流例例 讀一個(gè)文件并顯示出其中空格的位置讀一個(gè)文件并顯示出其中空格的位置#includevoid main( ) char ch; ifstream tfile (pay

13、roll,ios:binary |ios:nocreate); if(tfile) while (tfile.good( ) streampos here=tfile.tellg( ); tfile.get(ch); if(ch= ) coutnPositionhereis a space; else coutERROR:Cannot open file payrollendl; 文件文件payroll的內(nèi)容:的內(nèi)容: 123 44 5555 6 ios:goodPosition3 is a spacePosition6 is a spacePosition11 is a spacePosit

14、ion12 is a spacePosition13 is a space.ios:goodint good() const;Return ValueReturns a nonzero value if all error bits are clear. Note that the good member function is not simply the inverse of the bad function. 可以把流的概念連接到字符串(可以把流的概念連接到字符串(string)上。文件在)上。文件在C+看來(lái)是字節(jié)流或字符流,串當(dāng)然更可以看作字符流??磥?lái)是字節(jié)流或字符流,串當(dāng)然更可以看作

15、字符流??梢杂幂斎胼敵霾僮鱽?lái)完成串流的操作。其他的流是與設(shè)可以用輸入輸出操作來(lái)完成串流的操作。其他的流是與設(shè)備相關(guān),而串流是與內(nèi)存相關(guān),所以也稱內(nèi)存流。備相關(guān),而串流是與內(nèi)存相關(guān),所以也稱內(nèi)存流。 串流類包括串流類包括ostrstream、istrstream、strstream。它們?cè)谒鼈冊(cè)谥姓f(shuō)明。串流類對(duì)象可以保存字符,中說(shuō)明。串流類對(duì)象可以保存字符,也可以保存整數(shù)、浮點(diǎn)數(shù)。串流類對(duì)象采用文本方式,如也可以保存整數(shù)、浮點(diǎn)數(shù)。串流類對(duì)象采用文本方式,如同文本文件一樣。其構(gòu)造函數(shù)常用下面幾個(gè):同文本文件一樣。其構(gòu)造函數(shù)常用下面幾個(gè):istrstream:istrstream(const char * str);istrstream:istrstream(const char * str,int);ostrstream:ostrstream(char *,int,int=ios:out);以串流為信息源:char str36=”This is a book.n”;char ch;istrstream input(str);inputch;/從輸入設(shè)備(串)讀入一個(gè)字符coutchendl;/輸出T例:將整數(shù)和浮點(diǎn)數(shù)放入串中,并取出來(lái)。例:將整數(shù)和浮點(diǎn)數(shù)放入串中,并取出來(lái)。 i

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(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)論