c++ primer plus 第 17 章 输入、输出和文件
第 17 章 輸入、輸出和文件
17.1. C++輸入和輸出概述
C實現自帶了一個標準函數庫,C++自帶了一個標準類庫。標準類庫是一個非正式的標準,是由頭文件 iostream 和 fstream 中定義的類組成。
17.1.1 流、緩沖區和iostream
C++程序把輸入和輸出看作字節流。
- 輸入時
- 程序從輸入流中抽取字節
- 輸出時
- 程序把字節插入到輸出流中
輸入流中的字節可能來自鍵盤、存儲設備或者其它程序。
輸出流中的字節可以流向屏幕、打印機、存儲設備或者其它程序。
管理輸入包含兩步:
- 將流與輸入去向的程序關聯起來
- 將流與文件連接起來。
細致地講就是:輸入流要有兩個連接,每個端口一個。文件端連接提供了流的來源,程序端連接將輸出流的流出部分轉儲到程序中。
對輸出的管理:將輸出流連接到程序以及將輸出目標與流關聯起來。
通常,通過使用緩沖區可以高效地處理輸入和輸出。緩沖區是用作中介的內存塊 。它將信息從設備傳輸到程序或從程序傳輸給設備的 臨時存儲工具。
??沒有緩沖區的弊端:程序只能將文件中的內容一個字符一個字符讀取出來,這樣需要大量的磁盤活動,性能非常低。
?緩沖區的好處:磁盤以數據塊為單位傳輸信息,而程序每次只能處理一個字節的信息。緩沖區可以幫助**匹配二者的信息傳輸速率。**緩沖方法從磁盤中讀取大量的信息,并存儲到緩沖區(內存空間中預留了一定的存儲空間,這些存儲空間用來緩沖輸入或輸出的數據,這部分預留的空間就叫做緩沖區。它屬于內存的一部分)中,這樣程序從內存中讀取單個字節的速度比從磁盤上讀取要快得多。
刷新緩沖區(flushing the buffer):輸出時,程序首先填滿緩沖區,然后把整塊數據傳輸給硬盤,并清空緩沖區,以便下一批輸出使用。
📖tips:對鍵盤輸入進行緩沖可以讓用戶在將輸入傳輸給程序之前返回更正,(c++通常是在用戶按下回車鍵時刷新輸入緩沖區),這就是為什么用戶按下回車鍵(相當于:換行符)時程序才會運行,屏幕才會輸出(顯示結果)。
頭文件iostream 中包含了一些專門設計用來實現管理流和緩沖區的類。
- streambuf類 為緩沖區提供了內存,并提供了用于填充緩沖區,訪問緩沖區內容,刷新緩沖區和管理緩沖區內存的類方法。
- ios_base類 表示流的一般特征,如是否可讀取,是二進制還是文本流等。
- ios類基于ios_base,其中包含了一個指向streambuf對象的指針成員。
- ostream類 是基于ios類派生而來,提供輸出方法。
- istream類 也是基于ios類派生而來,提供輸入方法。
- iostream是基于 istream 和 ostream類,基礎了輸入和輸出方法。
在程序中包含 iostream 文件,將自動創建8個流對象(4個用于窄字符流,4個用于寬字符流)
- 處理窄字符流 ----- 處理 char_t 類型
- cin對象對應于標準輸入流。
- cout對象與標準輸出流相對應。
- cerr對象與標準錯誤流相對應,用于顯示錯誤信息。流不會被緩沖。
- clog對象對應標準錯誤流。流會被緩沖。
- 處理寬字符流 ----- 處理 wchar_t 類型
- wcin對象
- wcout對象
- wcerr對象
- wclog對象
17.1.2 重定向
標準輸入和輸出流通常連接鍵盤和屏幕。
- 輸入重定向(<)
- 輸出重定向(>)
假如有一名為counter.exe的可執行文件:
C>counter //input message Hello and goodbye! Control-Z << 模擬文件尾 Input contained 19 characters C>通過輸入重定向和輸出重定向來使上述文件符計算文件oklahoma中的字符數,并將結果放回cow_cnt文件中。
cow_cnt file: C>counter <oklahoma >cow_cnt C>解釋:<oklahoma將標準輸入與oklahoma文件連接,使cin從該文件(而不是鍵盤)中讀取輸入。>cow_cnt將標準輸出與cow_cnt文件連接,使cout將輸出發送給這個文件。
1??從第一點看來,操作系統可以改變輸入流的流入端連接,而流出端仍然與程序相連。
2??從第二點看來,操作系統可以改變輸出流的流出端連接,而流入端仍然與程序相連。
17.2.1 使用cout進行輸出
17.2.1.1 輸出和指針
C++將輸出流看作字節流。平臺不同,則讀取字節流會有差異。
ostream類最重要的任務之一:將數據內部表示(二進制位模式)轉換為由字符字節組成的輸出流,使能夠直接翻譯成二進制數據。
在C++中,<< 運算符的默認含義是按位左移運算符。但ostream類重新定義了 << 運算符,方法將其重載為輸出(也稱為 插入運算符)。
插入運算符的所有化身的返回類型都是 ostream&。原型格式如下:
ostream & operator<<(type);??C++用指向字符串存儲位置的指針來表示字符串,指針的形式可以是char數組名,顯式的char指針或用引用括起來的字符串
char name[20] = "Duddy Diddlemore"; char *pn = "Violet D'Amore"; cout << "Hello!"; cout << name; //Duddy Diddlemore cout << pn; //Violet D'Amore??如果要獲得字符串的地址,則必須將其強制轉換為其他類型,如下:
char *amount = "dozen"; cout << amount; cout << (void *) amount; //prints the address of the "dozen" string17.2.1.2 拼接輸出
函數定義指出:引用將指向用于該運算符的對象。
17.2.2 其他ostream方法
除了 operator<<() 函數外,ostream類還提供 put() 方法和 write() 方法。前者用于顯示字符,后者用于顯示字符串。
-
put()方法
- 原型如下:
cout是調用方法的對象,put()是類成員函數–該函數也返回一個指向對象的引用,所以可以用它拼接輸出:
cout.put('I').put('t'); //displaying It with two put() calls; -
write()方法
- 原型如下:
- ??注意點:write() 方法不會在遇到空字符時自動停止打印字符,而只是打印指定數目的字符,即使超出字符串的邊界。下面舉個例子看一下write的工作方式:
- write()方法也可用于數值數據,您可以將數字的地址強制轉換為char*,然后傳遞給它:
都是類成員函數,需要有調用對象
cout.put('w'); cout.write("Kansas",6);17.2.3 刷新輸出緩沖區
ostream類對cout對象處理的輸出進行緩沖,所以輸出不會立即發送到目標地址,而是被存儲到緩沖區中,直到緩沖區填滿。然后程序將刷新緩沖區,把內容發送出去,并清空緩沖區,以存儲新的數據。
如果實現不能在所希望時刷新輸出,可以使用強制刷新的兩個控制符之一:
- flush:刷新緩沖區
- endl:刷新緩沖區,并插入一個換行符
17.2.4 用cout進行格式化
ostream插入運算符將值轉換為文本格式。在默認情況下,格式化值的方式如下:
- char值:如果代表的是可打印字符,則將被作為一個字符顯示在寬度為一個字符的字段中。
- 數值整型:將以十進制方式顯示在一個剛好容納該數字的字段中。
- 字符串:顯示在寬度等于該字符串長度的字段中浮點類型:浮點類型被顯示為6位,末尾的0不顯示
- 數字以定點表示法顯示還是科學計數法表示,取決于值。
- 當指數大雨6或者小于等于-5時,將使用科學計數法。
- 字段寬度恰好容納數字和負號。
下面程序演示默認的輸出情況,ps:1.0/9.0位無窮小數
#include <iostream> int main() {using namespace std;cout << "12345678901234567890" << endl;char ch = 'k';int t = 273;cout << ch << ":\n";cout << t << ":\n";cout << -t << "\n";double f1 = 1.200;cout << f1 << ":\n";cout << (f1 + 1.0 / 9.0) << ":\n";double f2 = 1.67e2;cout << f2 << ":\n";f2 += 1.0 / 9.0;cout << f2 << ":\n";cout << (f2 * 1.0e4) << ":\n";double f3 = 2.3e-4;cout << f3 << ":\n";cout << f3 / 10 << ":\n";return 0; }輸出: 12345678901234567890 k: 273: -273 1.2: 1.31111: 167: 167.111: 1.67111e+06: 0.00023: 2.3e-05:??該實現將指數變為兩位(也有可能是三位,實現由不同編譯器而異),按照輸出看來,浮點型有效數字一般為6位。
17.2.4.1 修改顯示時使用的計數系統
ostream類從ios類派生而來,而ios從ios_base類派生而來。ios_base類存儲了描述格式狀態的信息。
通過使用 ios_base 的成員函數,可以控制字段和小數位數。。因ios_base類時ostream的間接基類,可以將其方法用于ostream對象。
要控制整數以十進制、十六進制還是八進制顯示,可以使用 dec、hex 和 oct 控制符。
dec(cout); // 十進制,等價于 cout << dec; hex(cout); // 十六進制,等價于 cout << hex; oct(cout); // 八進制,等價于 cout << oct;使用上述設置后,程序將以十六進制形式打印整數值,直到將格式狀態設置為其它選項為止。注意:控制符不是成員函數,不必通過對象來調用。下面程序演示了這些控制符的用法:
#include <iostream> int main(){using namespace std;cout << "Enter an integer:" ;int n;cin >> n;cout << "n n * n\n";cout << n << " " << n * n << "(decimal)\n";//set to hex;(十六進制)cout << hex;cout << n << " " << n * n << "(hexadecimal)\n";//set to octal(八進制)cout << oct;cout << n << " " << n * n << "(octal)\n";//函數調用dec(cout);cout << n << " " << n * n << "(decimal)\n";return 0; }輸出: Enter an integer:13 n n * n 13 169(decimal) d a9(hexadecimal) 15 251(octal) 13 169(decimal)17.2.4.2 調整字段寬度
由于數字的字段寬度不同,所以可以使用 width 成員函數將長度不同的數字放到寬度相同的字段中。方法原型為:
int width(); // 返回字段寬度的當前設置 int width(int i); // 將字段寬度設置為 i 個空格,并返回以前的字段寬度值width() 方法只影響將顯示的下一個項目,然后字段寬度將恢復為默認值。
C++永遠不會截短數據,會增寬字段,以容納該數據。C/C++的原則 :顯示所有的數據比保持列的整潔更重要,C++視內容重于形式。
int main() {cout.width(5);cout << "N" << ":"; }下面演示width()是如何工作的:
#include <iostream> int main(){using namespace std;int w = cout.width(30);cout << "default field width = " << w << "\n";cout.width(5);cout << "N" << ':';cout.width(8);cout << "N * N" << ":\n";for (long i = 1; i <= 100; i *= 10) {cout.width(5);cout << i << ":";cout.width(8);cout << i * i << "\n";}return 0;}輸出:default field width = 0N: N * N:1: 110: 100100: 10000解釋:看輸出結果可知,cout.width(30)返回的是原本的值,它將字段寬度設置為30只是影響下一個要輸出第一個字符串"default field width = "(這個字符串長度為22),故輸出的時候前面要填充8個空格以滿足字段寬度30。
17.2.4.3 填充字符
在默認情況下,cout 使用空格填充字段中未被使用的部分,可以使用 fill() 成員函數來改變填充字符。
cout.fill('*');對于檢查結果,防止接收方添加數字很有用。
下面演示該成員函數的用法:
#include <iostream>int main() {using namespace std;cout.fill('*');const char *staff[2] = {"Waldo Whipsnade", "Wilmarie Wooper"};long bonus[2] = {900, 1350};for (int i = 0; i < 2; ++i) {cout << staff[i] << ":$";cout.width(7);cout << bonus[i] << "\n";}return 0; }輸出: Waldo Whipsnade:$****900 Wilmarie Wooper:$***135017.2.4.4 設置浮點數的顯示精度
浮點數精度的含義取決于輸出模式。在默認模式下,指的是顯示的總位數。
在定點模式和科學模式下,精度指的是小數點后的位數。
C++中的默認精度為6位(末尾的0將不顯示),precision() 成員函數使能夠選擇其他值。
cout.precision(2); // 設置精度為2,設置后一直有效,只有重新設置會被重置下面程序演示該成員函數的用法:
#include <iostream> int main(){using namespace std;float price_1 = 20.40;float price_2 = 1.9 + 8.0 / 9.0;cout << "\"Furry Friends\" is $" << price_1 << "!\n";cout << "\"Fiery Friends\" is $" << price_2 << "!\n";cout.precision(2);cout << "\"Furry Friends\" is $" << price_1 << "!\n";cout << "\"Fiery Friends\" is $" << price_2 << "!\n";return 0; }輸出: "Furry Friends" is $20.4! "Fiery Friends" is $2.78889! "Furry Friends" is $20! "Fiery Friends" is $2.8!17.2.4.5 打印末尾的0或小數點
使用 setf() 函數,能夠控制多種格式化特性:
cout.setf(ios_base::showpoint); // 默認精度是6位使用默認的浮點格式時,會將導致末尾的0被顯示出來。
??showpoint是iOS_BASE類聲明中定義的類級靜態常量。類級意味著如果在成員函數定義的外面使用它,則必須在常量名前面加上作用域運算符(:😃。因此ios_base::showpoint指的是在ios_base類定義的一個常量。
在17.2.4.5中的程序里加入line1的代碼即可看到效果:
#include <iostream> int main(){using namespace std;float price_1 = 20.40;float price_2 = 1.9 + 8.0 / 9.0;cout.setf(ios_base::showpoint);cout << "\"Furry Friends\" is $" << price_1 << "!\n";cout << "\"Fiery Friends\" is $" << price_2 << "!\n";cout.precision(2);cout << "\"Furry Friends\" is $" << price_1 << "!\n";cout << "\"Fiery Friends\" is $" << price_2 << "!\n";return 0; }輸出: "Furry Friends" is $20.4000! "Fiery Friends" is $2.78889! "Furry Friends" is $20.! "Fiery Friends" is $2.8!輸出的第三行顯示了小數點,因為小數點前面已經包含兩位了。
17.2.4.6 setf()
ios_base類有一個受保護的數據成員,其中的各位分別控制著格式化的各個方面。
對于setf() 函數,有兩個原型:
// 此為原型1 fmtflags setf(fmtflags);fmtflags 是 bitmask類型的typedef名,用于存儲格式標記,該名稱是在ios_base中定義的。
其中ios_base定義了代表位置的常量,其中一些定義為:
因為都是 ios_base類中定義,所以使用時,必須加上作用域解析運算符。
#include <iostream> int main(){using std::cout;using std::endl;using std::ios_base;int temperature = 63;cout << "Today's water temperature:";cout.setf(ios_base::showpos);cout << temperature << endl;cout << "For our programming friend,that's\n";cout << std::hex << temperature << endl; //use 16進制cout.setf(ios_base::uppercase); //use uppercase in hexcout.setf(ios_base::showbase); //use 0X prefix(前綴) for hexcout << "or\n";cout << temperature << endl;cout << "How " << true << "! oops -- How ";cout.setf(ios_base::boolalpha);cout << true << "!\n";return 0; }輸出: Today's water temperature:+63 For our programming friend,that's 3f or 0X3F How 0X1! oops -- How true!??僅當十進制時才使用加號,c++將十六進制和八進制都視為無符號的。
bitmask 類型是一種用來存儲各個位值的類型??梢允钦汀⒚杜e,也可以是STL bitset容器。
第二種 setf() 函數的原型:
// 第一個參數:包含所需設置的fmtflags值 // 第二個參數:指出要清除第一個參數中的哪些位 fmtflags setf(fmtflags,fmtflags);清除位(clearing the bit):將第3位設置為1表示以10為基數,將第4位設置為1表示以8為基數,將第5位設置為1表示以16為基數。假設輸出是以10為基數,而要將它設置為16為基數,則不僅需要將第5位設置為1,還需要將第三位設置為0,這就叫作清除位。
例如,要左對齊,則使用:
ios_base::fmtflags old = cout::setf(ios::left, ios::adjustfield);下面函數調用和使用十六進制控制符的作用相同:
cout.setf(ios_base::hex,ios_base::basefield);定點表示法意味著使用格式123.4來表示浮點值,科學表示法則意味著使用格式1.23e04.對于printf()的說明符,則可能知道,定點表示法對應與%f說明符,而科學表示法對應于%e說明符。
如果要恢復以前的設置,則使用
cout.setf(old,ios::adjustfield);在調用setf() 后可以通過unsetf() 來消除,unsetf() 的原型如下:
// mask 是位模式,mask中是所有的位都設置為1,將使得對應的位被復位(置為0) void unsetf(fmtflags mask);其對應的用法如下:
cout.setf(ios_base::showpoint); //display trailing decimal point cout.unsetf(ios_base::boolshowpoint); //don't displaying trailing decimal point cout.setf(ios_base::boolalpha); //display true or false cout.unsetf(ios_base::boolalpha); //display 1 or 0在C++標準中,定點表示法和科學表示法都有兩個特征:
- 精度指的是小數位數,而不是總位數
- 顯示末尾的0
下面是有關setf()的版本二的相關用法:
#include <iostream> #include <cmath>int main(){using namespace std;//use left justification,show the plus sign,show trailing(后面的,尾隨)//zeros,with a precision of 3cout.setf(ios_base::left,ios_base::adjustfield);cout.setf(ios_base::showpos);cout.setf(ios_base::showpoint);cout.precision(3);// use e-notation(科學技術法) and save old format settingios_base::fmtflags old = cout.setf(ios_base::scientific,ios_base::floatfield);cout << "Left Justification:\n";long n;for (n = 1;n <= 41;n+=10){cout.width(4);cout << n << "|";cout.width(12);cout << sqrt(double(n)) << "|\n";}//change to internal justificationcout.setf(ios_base::internal,ios_base::adjustfield);//restore default floating-ponit display stylecout.setf(old,ios_base::floatfield);cout << "Internal Justification:\n";for (n = 1;n <= 41;n+=10){cout.width(4);cout << n << "|";cout.width(12);cout << sqrt(double(n)) << "|\n";}//use right justification ,fixed notationcout.setf(ios_base::right,ios_base::adjustfield);cout.setf(ios_base::fixed,ios_base::floatfield);cout << "Right Justification:\n";for (n = 1;n <= 41;n+=10){cout.width(4);cout << n << "|";cout.width(12);cout << sqrt(double (n)) << "|\n";}return 0; }Left Justification: +1 |+1.000e+00 | +11 |+3.317e+00 | +21 |+4.583e+00 | +31 |+5.568e+00 | +41 |+6.403e+00 | Internal Justification: + 1|+ 1.00| + 11|+ 3.32| + 21|+ 4.58| + 31|+ 5.57| + 41|+ 6.40| Right Justification:+1| +1.000|+11| +3.317|+21| +4.583|+31| +5.568|+41| +6.403|解釋:line13的語句存儲了之前浮點型的輸出結果的格式,其右值對下面的語句才開始起作用,就好比如之前學的cout.width(),line26的setf以原本的顯示模式作為第一參數(目標顯示模式),ios_base::floatfield作為第二參數。
17.2.4.7 標準控制符
17.2.4.8 頭文件iomanip
C++ 在頭文件 iomanip中提供了一些控制符。其中3個最常用的控制符分別是:
- setprecision():設置精度
- 接受一個指定精度的整數參數
- setfill():填充字符
- 接受一個指定填充字符的char參數
- setw():字符寬度
- 接受一個指定字段寬度的整數參數
下面對這些控制符進行簡單演示:
#include <iostream> #include <iomanip> #include <cmath> using namespace std; int main(){cout << fixed << right;//設置輸出方式:定點表示法、往右對齊// use iomanip munipulators(使用iomanip的控制符)cout << setw(4) << "N" << setw(14) << "Square root"<< setw(15) << "fourth root\n";double root;for(int i = 10 ;i <= 100 ;i += 10){root = sqrt(double (i));cout << setw(6) << setfill('~') << i << setfill(' ')<< setw(12) << setprecision(3) << root<< setw(13) << setprecision(4) << sqrt(root) << "\n";}return 0; }輸出:N Square root fourth root ~~~~10 3.162 1.7783 ~~~~20 4.472 2.1147 ~~~~30 5.477 2.3403 ~~~~40 6.325 2.5149 ~~~~50 7.071 2.6591 ~~~~60 7.746 2.7832 ~~~~70 8.367 2.8925 ~~~~80 8.944 2.9907 ~~~~90 9.487 3.0801 ~~~100 10.000 3.162317.3. 使用cin進行輸入
cin對象將標準輸入表示為字節流,通常情況下,通過鍵盤來生成這種字符流。
17.3.1 cin>>如何檢查輸入
不同版本的抽取運算符查看輸入流的方法都是相同的。他們跳過空白(空格、換行符和制表符),直到遇到非空白字符。
- 單字符模式
- >> 運算符將讀取該字符,將它放置到指定的位置
- 其他模式
- >> 運算符將讀取一個指定類型的數據。
對于下面的代碼:
int evolution; cin >> evolution;若向evolution中輸入-123Z,那么evolution只能取到3就截止了,因為Z字符對應int來說不是有效的字符,故其留在了輸入流里,下個cin將會從Z的位置開始讀取。
17.3.2 流狀態
流狀態(被定義為isolate類型,而isolate是一種bitmask類型)。由3個ios_base元素組成:
- eofbit:表示到達文件末尾
- badbit:遇到無法診斷的失敗破壞流
- failbit:未能讀取到預期的字符
其中的每個元素都是一位,可以是1(設置)或0(清除)。當3個狀態位都被設置為0時,說明一切順利。
clear() 和 setstate() 類型,都是重置狀態,但采取的方式不同。
- clear():將狀態設置為它的參數
- setstate():只影響其參數中設置的位置,而不會影響其他位。
exceptions() 方法返回一個位字段,包含3位,分別對應于eofbit、failbit、badbit。修改流狀態涉及clear() 或 setstate() ,都將使用clear()。當前狀態中的對應位也被設置,則clear() 將引發 ios_base::failure 異常。如果兩個值都設置了badbit,將發生這種情況。ios_base::failure是std::exception派生來的類,因此包含一個what()方法
exceptions() 的默認設置為 goodbit,沒有引發異常,但重載的 exceptions(isolate) 函數使得能夠控制其行為。
cin.exceptions(badbit);位運算符OR 使能夠指定多位。
cin.exceptions(badbit | eofbit);下面程序演示了其能夠在failbit被設置之后捕獲異常:
#include<iostream> #include<iomanip>using namespace std;int main() {int integerValue;cout << "Before a bad input operation:" << endl;cout << " cin.rdstate():" << cin.rdstate() << endl;cout << " cin.eof():" << cin.eof() << endl;cout << " cin.fail():" << cin.fail() << endl;cout << " cin.bad():" << cin.bad() << endl;cout << " cin.good():" << cin.good() << endl << endl;cout << " Expects an integer, but enter a character:" << endl;cin >> integerValue;cout << endl;cout << "After a bad input operation:" << endl;cout << " cin.rdstate():" << cin.rdstate() << endl;cout << " cin.eof():" << cin.eof() << endl;cout << " cin.fail():" << cin.fail() << endl;cout << " cin.bad():" << cin.bad() << endl;cout << " cin.good():" << cin.good() << endl << endl;cin.clear();cout << "After cin.clear()" << " cin.fail():" << cin.fail()<< " cin.good():" << cin.good() << endl;return 0; }輸出: Enter numbers: Before a bad input operation:cin.rdstate():0cin.eof():0cin.fail():0cin.bad():0cin.good():1Expects integers, but enter a character: 4 2 a ios_base::clear: unspecified iostream_category error After a bad input operation:cin.rdstate():4cin.eof():0cin.fail():1cin.bad():0cin.good():0The last input value: 0 ios::goodbit = 0 ios::badbit = 1 ios::eofbit = 2 ios::failbit = 4 Sum = 6??思考:為什么last input value為0?
可能由于(cin>>input)輸入的預期數據類型錯誤,failbit被置為1,因而導致了goodbit置為0,此時cin.good()–>input,導致了input最后的值為0而不是為2.(不同的編譯器可能不同,clion上是這樣的我認為,本書的例子給出的output的last_input_value 為 錯例輸入前的那個整數,放在這里就是2)。
補充一下:為什么goodbit、badbit、eofbit、failbit分別問0、1、2、4?
用cout檢測goodbit, badbit, eofbit, failbit的值分別是0,1,2,4,這與上面的表格正好完全對應著
(goodbit:0000 0000; badbit:0000 0001;eofbit:0000 0010; failbit:0000 0100)。
只有在流狀態的良好的情況(所有的位都被清楚),下面的測試中while的循環條件才會為true。設置流狀態位將對后面的輸入或者輸出關閉,直到位將被清除。
while (cin >> input) {sum += input; } // 可以在此處增加 clear() 來清除流狀態 //cout.clear(); cin >> input; // don't work如果希望程序在流狀態位被設置后能夠讀取后面的輸入,就必須將流狀態設置為良好??梢酝ㄟ^調用 clear() 來實現。導致輸入循環終止的不匹配輸入仍留在輸入隊列中,程序必須跳過它。
//在cin >> input前、cout.clear()之間增加 while(!isspace(cin.get())){continue; //get rid of badinput }is space()函數是一個cctype函數,它在參數是空白字符的時候返回true,這樣上面的語句能夠跳過當前輸入錯誤的字符or字符串。還有另一個辦法保證接下來的輸入不會出錯:丟棄行中的剩余部分。
while(cin.get() != '\n'){contiune;//get rid of line left }現在,假設循環是由于到達文件尾或者由于硬件故障而終止的,則處理錯誤輸入的代碼將顯得毫無意義。可以使用fail()方法檢測是否正確。(ps:fail()在failbit或badbit其一有問題都會返回true),故需要排除后面一種情況,下面看個例子:
while (cin >> input) {sum += input; } .... if(cin.fail() && !cin.bad() && !cin.eof()){cin.clear();while(!isspace(cin.get())){continue; //get rid of badinput} } else{cout << "I cannot go on!";exit(1); } cout << "Now new input:" << endl; cin >> input;17.3.3 其他istream類方法
- 方法get(char&) 和 get(void) 提供不跳過空白的單個字符輸入功能
- 函數 get(char*, int, char) 和 getline(char*, int, char)在默認情況下讀取整行而不是一個單詞。
都稱為 非格式化輸入函數(unformatted input functions)。都只是讀取字符輸入,而不會跳過空白,也不進行數據轉換。
17.3.3.1 單字符輸入
(1)成員函數get(char &):返回一個指向用于調用它的istream對象的引用
先來看一個程序:
int ct = 0; char ch; cin.get(ch); while(ch != '\n'){cout << ch;ct++;cin.get(ch); } cout << ct << endl; //輸入I C++ clearly.<enter>按下回車后,這行輸入將被發送給程序。上述程序段先讀入I,cout顯示它,并將ct遞增到1。接著,讀取I后面的空格字符,顯示它,并將ct遞增到2,這一過程將一直繼續下去,直到程序將回車鍵作為換行符處理,并終止循環。這里的get(ch),不僅考慮可打印字符,還考慮空格。
如果使用**>>**,那么代碼會跳過空格,這樣的話將不考慮空格,上面的例子輸出:IC++clearly.然而使用這個還有一個弊端,抽取運算符跳過了跳過了換行符,不會將換行符賦給ch,這樣while循環成了死循環。
對于下面的例子:
char c1,c2,c3; cin.get(c1).get(c2) >> c3;首先,cin.get(c1)將第一個輸入字符賦值給c1,返回調用對象cin,接著就可以對第二個字符賦值給c2,c3的輸入語句可壓縮為cin >> c3;
如果到達了文件尾(無論是真實的,或者是鍵盤仿真的),它都不會給其參數賦值,因為到了文件尾,沒有值可供給參數了。最重要的是該方法還調用setstate(failbit),導致cin的測試結果為false。
char ch; while (cin.get()){//process input }只要是持續存在有效輸入,那么它的返回值為都是cin,則while條件判其為true,到達文件末尾時,返回值判定為false,循環終止。
(2)成員函數get(void)
get(void)成員函數還讀取空白,但使用返回值來將輸入傳遞給程序,其函數返回類型為int(或者某種更大的整型),這樣下面語句編寫的時候會非法:
char c3; cin.get().get() >> c3; // invalid但下面的語句是合法的:
#include <iostream> #include <stdlib.h> int main(){using namespace std;char c1;int c2;c2 = cin.get(c1).get();cout <<"c1 = " << c1 << " c2 = " << c2 << endl;return 0; }輸出: //input:aA c1 = a c2 = 65(A)到達文件尾后,cin.get(void)都將返回值EOF----頭文件iostream提供的一個符號常量。所以為了避免不要的錯誤,ch需要聲明為int的類型,而非char類型,避免EOF無法用char類型來表示。
17.3.3.2 采取哪種單字符輸入形式:
對于 >>、get(char &) 或者 get(void)的選擇問題上,確定是否跳過空格,如果跳過空白則使用抽取運算符>>。
get() 方法會檢查程序每個字符??梢詫in.get()替換所有的getchar(),用cout.put(ch)替換所有的putchar()來將c程序轉換為c++程序
17.3.3.3 字符串輸入:getline()、get()和ignore()
對于字符串的輸入成員函數,getline() 成員函數和 get() 的字符串讀取版本都讀取字符串,他們的函數特征標相同。
// 第一個參數:放置輸入字符串的內存單元的地址 // 第二個參數:比要讀取的最大字符數大1(額外的一個字符用于存儲結尾的空字符,以便輸入存儲為一個字符串) // 第三個參數:指定用做分界符的字符 istream & get(char *, int, char); istream & get(char *, int); istream & getline(char *, int, char); istream & getline(char *, int);下面代碼將字符輸入讀取到字符數組line中:
char line[50]; cin.get(line,50);由上面的說明可知,cin.get()函數將在到達第49個字符或者遇到換行符后停止將輸入讀取到數組中。
對于get() 和 getline() 方法的區別:
- get():將換行符留在輸入流中
- getline():抽取并丟棄輸入流中的換行符。
特殊的成員函數 ignore(),函數原型表示:
istream & ignore(int = 1,int = EOF); // 原型中的兩個參數提供的默認值為 1 和 EOF。默認參數值EOF導致 ignore() 讀取指定數目的字符或讀取文件尾。
下面程序演示getline()、get()的使用方法,還順便介紹一個新的函數ignore。
#include <iostream> #include <string> const int Limit = 255; int main(){using namespace std;char input[Limit];cout << "Enter a string for getline() processing:\n" << endl;cin.getline(input,Limit,'#');cout << "Here is your input:\n";cout << input << "\nDone with phase 1\n";char ch;cin.get(ch);cout << "The next input character is " << ch << endl;if(ch != '\n'){//讀取并丟棄接下來的Limit個字符或直到到達第一個換行符(針對的是輸入流中殘存的字符流)cin.ignore(Limit,'\n');}cout << "Enter a string for get() procesiing:\n";cin.get(input,Limit,'#');cout << "Here is your input:\n";cout << input << "\nDone with phase 2\n";cin.get(ch);cout << "The text input character is " << ch << endl;return 0; }輸出: Enter a string for getline() processing: Please pass me a #3 melon! Here is your input: Please pass me a Done with phase 1 The next input character is 3 Enter a string for get() procesiing: I still want a #3 melon! Here is your input: I still want a Done with phase 2 The text input character is #17.3.3.4 意外字符串輸入
兩種特殊情況:無輸入以及輸入到達或超過函數調用指定的最大字符數。
對于get(char *,int)和getline(),如果不能抽取字符,它們將把空值字符放置到輸入串中,并使用setstate()設置為failbit。出現這種情況有兩種可能:
-
輸入方法立刻到達了文件尾
-
對于get(char *,int)來說,另一種可能是輸入了空行。
- char temp[80]; while (cin.get(temp,80)){ //terminates on empty line... }
-
getline()遇到空行不會設置failbit,這是因為getline()仍將抽取換行符,如果希望getline()遇到空行時就終止循環可以這樣寫:
- char tmep[80]; while(cin.getline(temp,80) && temp[0] != '\0') //terminates on empty line
-
getline()如果讀取0-79個字符,并且下一個字符不是換行符,則設置failbit,后續的字符無法輸入進來。
17.3.4 其他istream方法
- read():讀取指定數目的字節,并將其存儲在指定的位置中。不會在輸入后加上空值字符。(返回類型為istream&)
- peek():返回輸入中的下一個字符,但不抽取輸入流中的字符。
- gcount():返回最后一個非格式化抽取方法讀取的字符數。(strlen比它快)
- putback():將一個字符插入到輸入字符串中,被插入的字符將是下一條輸入語句讀取的第一個字符。(返回類型為istream&)
下面通過示例演示一下這種方法:
#include <iostream> #include <string> int main(){using namespace std;char ch;while (cin.get(ch)){ //terminate on EOFif (ch != '#'){cout << ch;}else{cin.putback(ch); //reinsert characterbreak;}}if (!cin.eof()){cin.get(ch);cout << endl << ch << " is next input character.\n";}else{cout << "End of file reached.\n";std::exit(0);}while (cin.peek() != '#'){cin.get(ch);cout << ch;}if(!cin.eof()){cin.get(ch);cout << endl << ch << " is next input character.\n";}else{cout << "End of file reached.\n";}return 0; }輸出: I used a #3 pencil and when I should have used a #2. I used a # is next input character. 3 pencil and when I should have used a # is next input character.下面的例子使用peek()來確定是否讀取了整行。如果一行中只有部分內容被加入到數組中,程序將刪除余下內容:
#include <iostream> #include <string>const int SLEN = 10;inline void eatline() {while (std::cin.get() != '\n') {continue;} }int main() {using namespace std;char name[SLEN];char title[SLEN];cout << "Enter your name:\n";cin.get(name, SLEN);if (cin.peek() != '\n') {cout << "Sorry,we only have enough room for " << name << endl;}eatline();cout << "Dear " << name << ", enter your title:\n";cin.get(title, SLEN);if (cin.peek() != '\n') {cout << "We were forced to truncate(截斷) your title.\n";}eatline();cout << " Name: " << name << "\nTitle: " << title << endl;return 0; }輸出: Enter your name: Ella Fishsniffer Sorry,we only have enough room for Ella Fish Dear Ella Fish, enter your title: Executive Adjunct We were forced to truncate(截斷) your title.Name: Ella Fish Title: Executive17.4. 文件輸入和輸出
C++ I/O類軟件包處理文件輸入和輸出的方式與處理標準輸入和輸出的方式相似。
- 要寫入文件,需要創建一個ofstream對象,并使用ofstream方法。
- 要讀取文件,需要創建一個ifstream對象,并使用ifstream方法。
17.4.1 簡單的文件I/O
要讓程序寫入文件的做法:
- 創建一個ofstream對象來管理輸出流
- 將該對象與特定的文件關聯起來
- 以使用cout的方式使用該對象,唯一的區別是輸出將進入文件,而不是屏幕。
讀取文件的做法:
- 創建一個ifstream對象來管理輸入流
- 將該對象與特定的文件關聯起來
- 以使用cin的方式使用該對象
當輸入和輸出流對象過期(如程序終止)時,到文件的連接將自動關閉,另外,也可以使用 close() 方法來顯式地關閉到文件的連接。關閉連接并不會刪除流,只是將流重新連接到同一個文件或者另一個文件。
ifstream fin; ofstream fout; fout.close(); fin.close();下面看一個簡單的例子:輸入文件名,并以該輸入的文件,將一些信息寫入到該文件中,然后關閉該文件。
#include <iostream> #include <fstream> #include <string>int main(){using namespace std;string fileName;cout << "Enter name for new file: ";cin >> fileName;//create output stream object for new file and call it foutofstream fout(fileName.c_str());fout << "For your eyes only:\n";cout << "Enter your secret number:\n";float secret;cin >> secret;fout << "Your secret number is " << secret << endl;fout.close();//create input stream object for new file and call it finifstream fin(fileName.c_str());cout << "Here are the contents of " << fileName << ":\n";char ch;while (fin.get()){cout << ch;}cout << "Done\n";fin.close();return 0; }下面在目錄下找到了相應的文件:
17.4.2 流狀態檢查和is_open()
C++文件流從 ios_base 類那里繼承了一個流狀態成員。在C++通過使用 is_open() 方法 來檢查文件是否被成功打開。
假如打開一個不存在的文件進行輸入時,將設置為failbit位,因此可以這樣寫:
fin.open("fileName"); if(fin.fail()){...} //or if(!fin){...} //or if(!fin.open()){...}17.4.3 命令行處理技術
文件處理程序通常是使用命令行參數來指定文件。命令行參數是用戶在輸入命令時,在命令行中輸入的參數。
//wc是程序名,后面三個參數是作為命令行參數傳遞給程序的文件名 wc report1 report2 report3C++中在命令行環境中運行的程序能夠訪問命令行參數的機制
// argc:命令行中的參數個數 // argv:變量為一個指針,指向一個指向char的指針。argv[0] 表示指向一個參數(字符串指針) int main(int argc, char *argv[])可以在命令行中打開main.cpp:
然后在終端進行命令行輸入:
g++ main.cpp //編譯,會產生一個a.out文件 ./a.out article1(這是文件)輸出: 43842 characters in ./a.out 3 characters in article1(存有一個漢字) 43845 characters in all files argc = 2補充:不同的編碼方式,漢字所占的字符是不同的,在國家標準GB2312中:一個漢字 = 2 個字符;在UTF-8中,一個漢字 = 3個字符。
17.4.4 文件模式
將流與文件關聯時,都可以指定文件模式的第二個參數
ifstream fin("banjo",mode1); ofstream fout(); fout.open("harp",mode2);文件模式描述的是文件將被如何使用:讀、寫、追加等。
ios_base類定義了一個 openmode 類型,用于表示模式。
位運算符OR(|):將兩個位值合并成一個可用于設置兩個位的值。
ofstream fout("bageis",ios_base::out | ios_base::app);在 ios_base::out 本身將導致文件將被截短,但與 ios_base::in 一起使用時,不會導致文件被截短,沒有列出的組合。
ios_base::ate 和 ios_base::app 都將文件指針指向打開的文件結尾。區別之處:
- ios_base::app:將指針放到文件尾
- ios_base::ate:只允許數據添加到文件尾。
下面是一些C++和C的打開模式
1、文件追加
下面程序演示在文件尾追加數據:
#include <iostream> #include <fstream> #include <string> #include <cstdlib>const char * file = "guest.txt"; int main(){using namespace std;char ch;ifstream fin;fin.open(file);if (fin.is_open()){cout << "Here are the current contents of the "<< file << " file:\n";while (fin.get(ch)){cout << ch;}fin.close();}//add new namesofstream fout(file,ios::out | ios::app);if (!fout.is_open()){cerr << "Can't open " << file << " file for output.\n";exit(EXIT_FAILURE);}cout << "Enter guest names (enter a blank line to quit):\n";string name;while (getline(cin,name) && name.size() > 0){fout << name << endl;}fout.close();//show revised filefin.clear(); //not necessary for some compilersfin.open(file);if (fin.is_open()){cout << "Here are the new contents of the "<< file << " file:\n";while (fin.get(ch)){cout << ch;}fin.close();}cout << "Done.\n";return 0; }輸出: Enter guest names (enter a blank line to quit): Genghis Kant Hank Attila Charles BiggHere are thee new contents of the guest.txt file: Genghis Kant Hank Attila Charles Bigg Done.二次執行: Here are the current contents of the guest.txt file: Genghis Kant Hank Attila Charles Bigg Enter guest names (enter a blank line to quit): Greta Greppo LaDonna Mobile Fannie MaeHere are the new contents of the guest.txt file: Genghis Kant Hank Attila Charles Bigg Greta Greppo LaDonna Mobile Fannie Mae Done.2、二進制文件
數據存儲在文件中,是兩種形式:
- 文本格式:將所有的內容都存儲為文本。
- 二進制格式:將存儲值的計算機內部表示。
- 計算機不是存儲字符,而是以二進制的形式存儲。
文件兩種存儲形式的優缺點
- 文本格式
- 優點
- 便于讀取
- 使用編輯器或字符處理器來讀取和編輯
- 優點
- 二進制格式
- 優點
- 不會有轉換誤差或者舍入誤差
- 保存速度快,不需要轉換
- 可以大塊存儲數據。
- 數據特征不同,空間占用小
- 缺點
- 換系統或者OS會導致無法正常使用
- 優點
要以二進制格式(而不是文本格式)存儲數據,可以使用 write() 成員函數。
將文件使用二進制的格式:
fout.write((char *) &pl, sizeof pl) // 將pl地址強制轉換為指向char的指針 // sizeof() 獲取字節數 // 這條語句導致程序前往pl的地址,并將開始的36個字節復制到與fout相關聯的文件。要使用文件恢復信息,則使用通過 ifstream對象 使用相應的 read() 方法:
ifstream fin("planets.dat",ios_base::in | ios_base::binary); fin.read((char *) &pl,sizeof pl); //這將從文件中復制sizeof pl個字節到pl結構中read() 和 write() 成員函數的功能是相反,一般使用 read() 來恢復 write() 寫入的數據。
下面程序使用以上方法創建和讀取二進制文件
#include <iostream> #include <fstream> #include <iomanip> #include <cstdlib> //for exit()inline void eatline(){while (std::cin.get() != '\n'){continue;} }struct planet{char name[20];double population;double g; //its acceleration of gravity };const char* file = "planets.dat"; int main(){using namespace std;planet pl;cout << fixed;//show initial contentsifstream fin;fin.open(file);if (fin.is_open()){cout << "Here are the current contents of the "<< file << " file:\n";while (fin.read((char *) &pl,sizeof pl)){cout << setw(20) << pl.name << ":"<< setprecision(0) << setw(12) << pl.population<< setprecision(0) << setw(6) << pl.g;}fin.close();}//add new dataofstream fout(file,ios::out | ios::app | ios::binary);if(!fout.is_open()){cerr << "Cannot open " << file << " file for output:\n";exit(EXIT_FAILURE);}cout << "Enter planet name (enter a blank line to quit):\n";cin.get(pl.name,20);//'\0'代表字符串的末尾while (pl.name[0] != '\0'){eatline();cout << "Enter planetary population: ";cin >> pl.population;cout << "planet's acceleration of gravity: ";cin >> pl.g;eatline();fout.write((char *) &pl,sizeof pl);cout << "Enter planet name (enter a blank line to quit):\n";cin.get(pl.name,20);}fout.close();//show revised filefin.clear();fin.open(file);if (fin.is_open()){cout << "Here are the current contents of the "<< file << " file:\n";while (fin.read((char *) &pl,sizeof pl)){cout << setw(20) << pl.name << ":"<< setprecision(0) << setw(12) << pl.population<< setprecision(0) << setw(6) << pl.g;}fin.close();}cout << "Done\n";return 0;}//首次執行: Enter planet name (enter a blank line to quit): Earth Enter planetary population: 6928198253 planet's acceleration of gravity: 9.81 Enter planet name (enter a blank line to quit):Here are the current contents of the planets.dat file:Earth: 6928198253 10 Done//第二次執行 Here are the current contents of the planets.dat file:Earth: 6928198253 10 Enter planet name (enter a blank line to quit): Jenny's world Enter planetary population: 32155648 planet's acceleration of gravity: 8.93 Enter planet name (enter a blank line to quit):Here are the current contents of the planets.dat file:Earth: 6928198253 10Jenny's world: 32155648 9 Done??:是否可以使用string來替代字符數組來表示planet中的name。在不做出比較大的修改時答案是否定的。因為string本身是沒有字符串的,而是包含一個指向存儲字符串的內存單元的指針,故復制的時候,只會復制字符串的地址而不是字符串的數據。
17.4.5 隨機存取
隨機存取指的是直接移動(不是依次移動)到文件的任何位置。
隨機存取常被用于數據庫文件,程序維護一個獨立的索引文件。文件指出數據在主數據文件中的位置。
需要在一種文件中移動的方式,ifstream類繼承的兩個方法:
-
hiseekg():將輸入指針移到指定的文件位置。
- 可用于 ifstream 對象
-
seekp():將輸出指針移到指定的xing 文件位置。
- 可用于 ofstream對象
seekg() 的原型:
basic_istream<charT, traits> & seekg(off_type, ios_base::seekdir);//原型1 basic_istream<charT, traits> & seekg(pos_type);//原型2// 將 char 具體化 istream & seekg(streamoff, ios_base::seekdir); // 定位到離第二個參數指定的文件位置特定距離(單位為字節)的位置 istream & seekg(streampos); // 定位到離開文件開頭特定距離(單位為字節)的位置下面是一些關于原型1調用的示例:
ps:常量ios_base::cur指相對于當前位置的偏移量;ios_base::end指相對于文件尾的偏移量。
fin.seekg(30,ios_base::beg); //30 bytes beyond the beginning fin.seekg(-1,ios_base::cur); //back up one byte fin.seekg(0,ios_base::end); //go to the end of the file下面是一些關于原型2調用的示例:
fin.seekg(112); //第113個字節下面程序對17.4.4進行優化,所用的流既可以輸出也可以輸入,該程序的功能為修改特定位置的二進制數據:
#include <iostream> #include <fstream> #include <iomanip> #include <cstdlib>const int LIM = 20; struct planet {char name[LIM];double population;double g; };const char *file = "planets.dat";inline void eatline() {while (std::cin.get() != '\n') {continue;} }int main() {using namespace std;planet pl;cout << fixed;//show initial contentsfstream finout; //read and write streamsfinout.open(file, ios_base::in | ios_base::out | ios_base::binary);int ct = 0;if (finout.is_open()) {finout.seekg(0); //go to beginningcout << "Here are the current contents of the " << file << " file:\n";while (finout.read((char *) &pl, sizeof pl)) {cout << ct++ << ": " << setw(LIM) << pl.name << ": "<< setprecision(0) << setw(12) << pl.population<< setprecision(2) << setw(6) << pl.g << endl;}if (finout.eof()) {finout.clear();} else {cerr << "Error in reading " << file << ".\n";exit(EXIT_FAILURE);}} else {cerr << file << " could not be opened -- bye.\n";exit(EXIT_FAILURE);}//change a recordcout << "Enter the record number you wish to change: ";long rec;cin >> rec;eatline();if (rec < 0 || rec >= ct) {cerr << "Invalid record number -- bye.\n";exit(EXIT_FAILURE);}streampos place = rec * sizeof pl; //convert to streampos typefinout.seekg(place); //random accesif (finout.fail()) {cerr << "Error on attempted seek\n";exit(EXIT_FAILURE);}finout.read((char *) &pl, sizeof pl);cout << "Your selection:\n";cout << rec << ": " << setw(LIM) << pl.name << ": "<< setprecision(0) << setw(12) << pl.population<< setprecision(2) << setw(6) << pl.g << endl;if (finout.eof()){finout.clear(); //clear eof flag}cout << "Enter planet name:";cin.get(pl.name,20);eatline();cout << "Enter planetary population: ";cin >> pl.population;cout << "Enter planer's acceleration of gravity: ";cin >> pl.g;finout.seekg(place);finout.write((char *) &pl,sizeof pl) << flush;if (finout.fail()){cerr << "Error on attempted write\n";exit(EXIT_FAILURE);}ct = 0;finout.seekg(0); //go to beginning of file;cout << "Here are the new contents of the " << file << " file: \n";while (finout.read((char *) &pl,sizeof pl)){cout << ct++ << ": " << setw(LIM) << pl.name << ": "<< setprecision(0) << setw(12) << pl.population<< setprecision(2) << setw(6) << pl.g << endl;}finout.close();cout << "Done.\n";return 0; }輸出: Here are the current contents of the planets.dat file: 0: Earth: 6928198253 9.81 1: Tranmtor: 895218447777 10.53 2: Jenny's World: 32155648 8.93 3: Trellan: 5214000 9.62 4: Freestone: 3945851000 8.68 5: Taanagoot: 361000004 10.23 6: Marin: 252409 9.79 Enter the record number you wish to change: 1 Your selection: 1: Tranmtor: 895218447777 10.53 Enter planet name:Carlie Enter planetary population: 24398573451 Enter planer's acceleration of gravity: 8.77 Here are the new contents of the planets.dat file: 0: Earth: 6928198253 9.81 1: Carlie: 24398573451 8.77 2: Jenny's World: 32155648 8.93 3: Trellan: 5214000 9.62 4: Freestone: 3945851000 8.68 5: Taanagoot: 361000004 10.23 6: Marin: 252409 9.79 Done.17.5 內核格式化
讀取string對象中的格式化信息或將格式化信息寫入string對象中被稱為內核格式化。
頭文件 sstream 定義了一個從 ostream類派生來的ostringstream類。如果創建了一個ostringstream對象,則可以將信息寫入其中,并將其信息存儲。
istringstream 和 ostringstream類使得能夠使用 istream 和 ostream 類的方法來管理存儲在字符串中的字符數據。
下面是一個有關內核格式化的簡短示例:
#include <iostream> #include <sstream> #include <string> int main(){using namespace std;ostringstream outstr; //manages astring streamstring hardDisk;cout << "What's the name of your hard disk? ";getline(cin,hardDisk);int cap;cout << "What's its capacity in GB?";cin >> cap;//write formatted information to string streamoutstr << "The hard disk " << hardDisk << " has a capacity of "<< cap << " gigabytes.\n";string result = outstr.str();cout << result;return 0; }輸出: What's the name of your hard disk? Datarapture What's its capacity in GB?2000 The hard disk Datarapture has a capacity of 2000 gigabytes.使用str()方法可以“凍結”該對象,使得信息不能寫入該對象中
下面使用重載的>>運算符讀取字符串中的內容,每次讀取一個單詞:
#include <iostream> #include <sstream> #include <string> int main(){using namespace std;string lit = "It was a dark and stromy day,and the full moon glowed brilliantly.";string num = "1 2 3 4 6 8";int n,sum = 0;istringstream instr(lit);istringstream inNum(num);string word;;cout << "string: " << endl;while (instr >> word){ //read a word a timecout << word << endl;}while (inNum >> n){sum += n;}cout << "sum = " << sum << endl;return 0; }輸出: string: It was a dark and stromy day,and the full moon glowed brilliantly. sum = 24總結
以上是生活随笔為你收集整理的c++ primer plus 第 17 章 输入、输出和文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 构建数据科学档案:机器学习项目
- 下一篇: [C++] 结构体Struct类型和变量