?? 11章 c++輸入輸出流.txt
字號:
11 cin >> x >> y;
12 cout << "Sum of" << x << "and "<< y << "is:"
13 << ( x + y ) << endl;
14
15 return 0;
16 }
輸出結果:
Enter two integers: 30 92
Sum of 30 and 92 is: 122
圖 11.9 計算用cin和流讀取運算符從鍵盤輸入的兩個整數值的和
如果運算符>>和<<的優先級相對較高就會出現問題。例如,在圖11.10的程序中,如果條件表達式沒有用括號括起來,程序就得不到正確的編譯(讀者可以試一下)。
1 // Fig. 11.10: figlll0.cpp
2 // Avoiding a precedence problem between the stream-insertion
3 // operator and the conditional operator.
4 // Need parentheses around the conditional expression.
5 #include <iostream.h>
6
7 int main()
8
9 int x, y;
10
11 cout << "Enter two integers: ";
12 cin >> x >> y;
13 cout << x << ( x == y ? "is" : "is not" )
14 << "equal to "<< y << endl;
15
16 return 0;
17 }
輸出結果:
Enter two integers: 7 5
7 is not equal to 5
Enter two integers: 8 8
8 is equal to 8
圖 11.10 避免在流插入運算符和條件運算符之間出現優先級錯誤
常見編程錯誤 11.1
試圖從類ostream的對象(或其它只有輸出功能的流)中讀取數據
常見編程錯誤 11.2
試圖把數據寫入類istream的對象(或其它只有輸入功能的流)
常見編程錯誤 11.3
在流插入運算符<<或流讀取運算符>>的優先級相對較高時沒有用圓括號強制實現正確的計算順序
我們通常在while循環結構的首部用流讀取運算符輸入一系列值。當遇到文件結束符時,讀取運算符返回0false)。圖11.11中的程序用于查找某次考試的最高成績。假定事先不知道有多少個考試成績,并且用戶會輸入表示成績輸入完畢的文件結束符。當用戶輸入文件結束符時,while循環結構中的條件(cin>>grade)將變為0(即false)。
可移植性提示11.1
提示用戶如何從鍵盤結束輸入時,讓用戶輸入文件結束符結束輸入,而不是提示輸入<ctrl>-d(UNIX與Macintosh所使用的)或<ctrl-z(PC與VAX所使用的)。
在圖11. 11中,cin>>grade可以作為條件,因為基類ios(繼承istream的類)提供一個重載的強制類型轉換運算符,將流變成void*類型的指針。如果讀取數值時發生錯誤或遇到文件結束符,則指針值為0。編譯器能夠隱式使用void*類型的強制轉換運算符。
1 // Fig. 11.11: figll_ll.cpp
2 // Stream-extraction operator returning false on end-of-file.
3 #include <iostream.h>
4
5 int main()
6 {
7 int grade, highestGrade = -1;
8
9 cout << "Enter grade (enter end-of-file to end): ";
10 while ( cin >> grade){
11 if ( grade > highestGrade )
12 highestGrade = grade;
13
14 cout << "Enter grade (enter end-of-file to end): ";
15 }
16
17 cout << "\n\nHighest grade is: "<< highestGrade << endl;
18 return 0;
19 }
輸出結果:
Enter grade (enter end-of-file to end): 67
Enter grade (enter end-of-file to end): 87
Enter grade (enter end of file to end): 73
Enter grade (enter end-of-file to end): 95
Enter grade (enter end-of-file to end): 34
Enter grade (enter end-of-file to end): 99
Entergrade (enter end-of-file to end): ^ z
Heighest grade is: 99
圖 11.11 流讀取運算符在遇到文件結束符時返回false
11.4.2 成員函數get和getline
不帶參數的get函數從指定的輸入流中讀取(輸入)一個字符(包括空白字符),并返回該字符作為函數調用的值;當遇到輸入流中的文件結束符時,該版本的get函數返回EOF。
圖11.12中的程序把成員函數eof和get用于輸入流cin,把成員函數put用于輸出漉cout。程序首先輸出了cin.eof()的值,輸出值為0(false)表明沒有在cin中遇到文件結束符。然后,程序讓用戶鍵入以文件結束符結尾的一行文本(在IBMPC兼容系統中,文件結束符用<ctrl>-z表示;在UNIX和Macintosh系統中,文件結束符用<ctrl>-d表示)。程序逐個讀取所輸入的每個字符,井調用成員函數put將所讀取的內容送往輸出流cout。當遇到文件結束符時,while循環終止,輸出cin.eof()的值,輸出值為1(true)表示已接收到了cin中的文件結束符。注意,程序中使用了類istream中不帶參數
的get成員函數,其返回值是所輸入的字符。
1 // Fig. 11.12: figll_12.cpp
2 // Using member functions get, put and eof.
3 #include <iostream.h>
4
5 int main()
6 {
7 char c;
8
9 cout << "Before input, cin.eof() is "<< cin.eof()
10 << "\nEnter a sentence followed by end-of-file:\n";
11
12 while ( ( c = cin.get() ) != EOF )
13 cout.put( c );
14
15 cout << "\nEOF in this system is: "<< c;
16 cout << "\nAfter input, cin.eof() is "<< cin.eof() << endl;
17 return 0;
18 }
輸出結果:
Before input, cin.eof() is 0
Enter a sentence followed by end-of-file:
Testing the get and put member functions^z
Testing the get and put member functions
EOF in this system is: -1
After input cin.eof() is 1
圖 11.12 使用成員函數get.put和eof
帶一個字符型參數的get成員函數自動讀取輸人流中的下一個字符(包括空白字符)。當遇到文件結束符時,該版本的函數返回0,否則返回對istream對象的引用,并用該引用再次調用get函數。
帶有三個參數的get成員函數的參數分別是接收字符的字符數組、字符數組的大小和分隔符(默認值為'\n')。函數或者在讀取比指定的最大字符數少一個字符后結束,或者在遇到分隔符時結束。為使字符數組(被程序用作緩沖區)中的輸入字符串能夠結束,空字符會被插入到字符數組中。函數不把分隔符放到字符數組中,但是分隔符仍然會保留在輸人流中。圖11.13的程序比較了cin(與流讀取運算符一起使用)和cin.get的輸入結果。注意調用cin.get時未指定分隔符,因此用默認的'\n'。
1 // Fig. 11.13: figll 13.cpp
2 // contrasting input of a string with cin and cin.get.
3 #include <iostream.h>
4
5 int main()
6{
7 const int SIZE = 80;
8 char buffer1[ SIZE ], buffer2[ SIZE ] ;
9
10 cout << "Enter a sentence:\n";
11 cin >> buffer1;
12 cout << "\nThe string read with cin was:\n"
13 << buffer1 << "n\n";
14
15 cin.get( buffer2, SIZE );
16 cout << "The string read with cin.get was:\n"
17 << buffer2 << endl;
18
19 return 0;
20 }
輸出結果:
Enter a sentence:
Contrasting string input with cin and cin.get
The string read with cin was:
Contrasting
The string read with cin.get was:
string input with cin and cin.get
圖 11.13 比較用cin和cin.get輸入的字符串的結果
成員函數getline與帶三個參數的get函數類似,它讀取一行信息到字符數組中,然后插入一個空字符。所不同的是,getline要去除輸入流中的分隔符(即讀取字符并刪除它),但是不把它存放在字符數組中。圖11.14中的程序演示了用getline輸入一行文本。
1 // Fig. 11.14: figll_14.cpp
2 // Character input with member function getline.
3 #include <iostream.h>
4
5 int main()
6{
7 const SIZE = 80;
8 char buffe[ SIZE ];
9
10 cout << "Enter a sentence:\n";
11 cin.getline( buffer, SIZE );
12
13 cout << "\nThe sentence entered is:\n" << buffer << endl;
14 return 0;
15 }
輸出結果:
Enter a sentence:
Using the getline member function
The sentence entered is:
Using the getline member function
圖 11.14 用成員函數getline輸入字符
11.4.3 istream類中的其他成員函數(peek、putback和ignore)
成員函數ignore用于在需要時跳過流中指定數量的字符(默認個數是1),或在遇到指定的分隔符(默認分隔符是EOF,使得ignore在讀文件的時候跳過文件末尾)時結束。
成員函數putback將最后一次用get從輸人流中提取的字符放回到輸入流中。對于某些應用程序.該函數是很有用的。例如,如果應用程序需要掃描輸入流以查找用特定字符開始的域,那么當輸入該字符時,應用程序要把該字符放回到輸入流中,這樣才能使該字符包含到要被輸入的數據中。
成員函數peek返回輸入流中的下一個字符,但并不將其從輸入流中刪除。
11.4.4 類型安全的I/O
C++提供類型安全的I/O。重載運算符<<和>>是為了接收指定類型的數據。如果接收了非法的數據類型,那么系統就會設置各種錯誤標志,用戶可通過測試這些標志判斷I/O操作的成功與失敗。這種處理方法能夠使程序保持控制權。11.8節要討論這些錯誤標志。
11.5 成員函數read、gcount和write的無格式輸入/輸出
調用成員函數read、write可實現無格式輸入/輸出。這兩個函數分別把一定量的字節寫入字符數組和從字符數組中輸出。這些字節都是未經任何格式化的,僅僅是以原始數據形式輸入或輸出。
例如:
char buffe[] ="HAPPY BIRTHDAY";
cout.write(buffer, 10 );
輸出buffet的10個字節(包括終止cout和<<輸出的空字符)。因為字符串就是第一個字符的地址,所以函數調用:
cout.write("ABCDEFGHIJKLMNOPQRSTUVWXYZ",10);
顯示了字母表中的前10個字母。
成員函數read把指定個數的字符輸入到字符數組中。如果讀取的字符個數少于指定的數目,可以設置標志位failbit(見11.8節)。
成員函數gcount統計最后輸入的字符個數。
圖11.15中的程序演示了類istream中的成員函數read和gcount,以及類ostream中的成員函數write。程序首先用函數read向字符數組buffer中輸入20個字符(輸入序列比字符數組長),然后用函數gcount統計所輸入的字符個數,最后用函數write輸出buffer中的字符。
1 // Fig. 11.15: fig11_15.cpp
2 // Unformatted I/O with read, gcount and write.
3 #include <iostream.h>
4
5 int main()
6 {
7 const int SIZE = 80;
8 char buffer[ SIZE ];
9
10 cout << "Enter a sentence:\n";
11 cin.read( buffer, 20 );
12 cout << "\nThe sentence entered was:\n";
13 cout.write( buffer, cin.gcount() );
14 cout << endl;
15 return 0;
16 }
輸出結果:
Enter a sentence:
Using the read, write, and gcount member functions
The sentence entered was:
Using the read,write
圖 11.15 成員函數read,gcount和write的無格式I/O
11.6 流操縱算子:
C++提供了大量的用于執行格式化輸入/輸出的流操縱算子。流操縱算子提供了許多功能,如設置域寬、設置精度、設置和清除格式化標志、設置域填充字符、刷新流、在輸出流中插入換行符并刷新該流、在輸出流中插入空字符、跳過輸入流中的空白字符等等。下面幾節要介紹這些特征。
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -