c++ primer plus 第 17 章 输入、输出和文件
第 17 章 輸入、輸出和文件
17.1. C++輸入和輸出概述
C實(shí)現(xiàn)自帶了一個(gè)標(biāo)準(zhǔn)函數(shù)庫,C++自帶了一個(gè)標(biāo)準(zhǔn)類庫。標(biāo)準(zhǔn)類庫是一個(gè)非正式的標(biāo)準(zhǔn),是由頭文件 iostream 和 fstream 中定義的類組成。
17.1.1 流、緩沖區(qū)和iostream
C++程序把輸入和輸出看作字節(jié)流。
- 輸入時(shí)
- 程序從輸入流中抽取字節(jié)
- 輸出時(shí)
- 程序把字節(jié)插入到輸出流中
輸入流中的字節(jié)可能來自鍵盤、存儲(chǔ)設(shè)備或者其它程序。
輸出流中的字節(jié)可以流向屏幕、打印機(jī)、存儲(chǔ)設(shè)備或者其它程序。
管理輸入包含兩步:
- 將流與輸入去向的程序關(guān)聯(lián)起來
- 將流與文件連接起來。
細(xì)致地講就是:輸入流要有兩個(gè)連接,每個(gè)端口一個(gè)。文件端連接提供了流的來源,程序端連接將輸出流的流出部分轉(zhuǎn)儲(chǔ)到程序中。
對(duì)輸出的管理:將輸出流連接到程序以及將輸出目標(biāo)與流關(guān)聯(lián)起來。
通常,通過使用緩沖區(qū)可以高效地處理輸入和輸出。緩沖區(qū)是用作中介的內(nèi)存塊 。它將信息從設(shè)備傳輸?shù)匠绦蚧驈某绦騻鬏斀o設(shè)備的 臨時(shí)存儲(chǔ)工具。
??沒有緩沖區(qū)的弊端:程序只能將文件中的內(nèi)容一個(gè)字符一個(gè)字符讀取出來,這樣需要大量的磁盤活動(dòng),性能非常低。
?緩沖區(qū)的好處:磁盤以數(shù)據(jù)塊為單位傳輸信息,而程序每次只能處理一個(gè)字節(jié)的信息。緩沖區(qū)可以幫助**匹配二者的信息傳輸速率。**緩沖方法從磁盤中讀取大量的信息,并存儲(chǔ)到緩沖區(qū)(內(nèi)存空間中預(yù)留了一定的存儲(chǔ)空間,這些存儲(chǔ)空間用來緩沖輸入或輸出的數(shù)據(jù),這部分預(yù)留的空間就叫做緩沖區(qū)。它屬于內(nèi)存的一部分)中,這樣程序從內(nèi)存中讀取單個(gè)字節(jié)的速度比從磁盤上讀取要快得多。
刷新緩沖區(qū)(flushing the buffer):輸出時(shí),程序首先填滿緩沖區(qū),然后把整塊數(shù)據(jù)傳輸給硬盤,并清空緩沖區(qū),以便下一批輸出使用。
📖tips:對(duì)鍵盤輸入進(jìn)行緩沖可以讓用戶在將輸入傳輸給程序之前返回更正,(c++通常是在用戶按下回車鍵時(shí)刷新輸入緩沖區(qū)),這就是為什么用戶按下回車鍵(相當(dāng)于:換行符)時(shí)程序才會(huì)運(yùn)行,屏幕才會(huì)輸出(顯示結(jié)果)。
頭文件iostream 中包含了一些專門設(shè)計(jì)用來實(shí)現(xiàn)管理流和緩沖區(qū)的類。
- streambuf類 為緩沖區(qū)提供了內(nèi)存,并提供了用于填充緩沖區(qū),訪問緩沖區(qū)內(nèi)容,刷新緩沖區(qū)和管理緩沖區(qū)內(nèi)存的類方法。
- ios_base類 表示流的一般特征,如是否可讀取,是二進(jìn)制還是文本流等。
- ios類基于ios_base,其中包含了一個(gè)指向streambuf對(duì)象的指針成員。
- ostream類 是基于ios類派生而來,提供輸出方法。
- istream類 也是基于ios類派生而來,提供輸入方法。
- iostream是基于 istream 和 ostream類,基礎(chǔ)了輸入和輸出方法。
在程序中包含 iostream 文件,將自動(dòng)創(chuàng)建8個(gè)流對(duì)象(4個(gè)用于窄字符流,4個(gè)用于寬字符流)
- 處理窄字符流 ----- 處理 char_t 類型
- cin對(duì)象對(duì)應(yīng)于標(biāo)準(zhǔn)輸入流。
- cout對(duì)象與標(biāo)準(zhǔn)輸出流相對(duì)應(yīng)。
- cerr對(duì)象與標(biāo)準(zhǔn)錯(cuò)誤流相對(duì)應(yīng),用于顯示錯(cuò)誤信息。流不會(huì)被緩沖。
- clog對(duì)象對(duì)應(yīng)標(biāo)準(zhǔn)錯(cuò)誤流。流會(huì)被緩沖。
- 處理寬字符流 ----- 處理 wchar_t 類型
- wcin對(duì)象
- wcout對(duì)象
- wcerr對(duì)象
- wclog對(duì)象
17.1.2 重定向
標(biāo)準(zhǔn)輸入和輸出流通常連接鍵盤和屏幕。
- 輸入重定向(<)
- 輸出重定向(>)
假如有一名為counter.exe的可執(zhí)行文件:
C>counter //input message Hello and goodbye! Control-Z << 模擬文件尾 Input contained 19 characters C>通過輸入重定向和輸出重定向來使上述文件符計(jì)算文件oklahoma中的字符數(shù),并將結(jié)果放回cow_cnt文件中。
cow_cnt file: C>counter <oklahoma >cow_cnt C>解釋:<oklahoma將標(biāo)準(zhǔn)輸入與oklahoma文件連接,使cin從該文件(而不是鍵盤)中讀取輸入。>cow_cnt將標(biāo)準(zhǔn)輸出與cow_cnt文件連接,使cout將輸出發(fā)送給這個(gè)文件。
1??從第一點(diǎn)看來,操作系統(tǒng)可以改變輸入流的流入端連接,而流出端仍然與程序相連。
2??從第二點(diǎn)看來,操作系統(tǒng)可以改變輸出流的流出端連接,而流入端仍然與程序相連。
17.2.1 使用cout進(jìn)行輸出
17.2.1.1 輸出和指針
C++將輸出流看作字節(jié)流。平臺(tái)不同,則讀取字節(jié)流會(huì)有差異。
ostream類最重要的任務(wù)之一:將數(shù)據(jù)內(nèi)部表示(二進(jìn)制位模式)轉(zhuǎn)換為由字符字節(jié)組成的輸出流,使能夠直接翻譯成二進(jìn)制數(shù)據(jù)。
在C++中,<< 運(yùn)算符的默認(rèn)含義是按位左移運(yùn)算符。但ostream類重新定義了 << 運(yùn)算符,方法將其重載為輸出(也稱為 插入運(yùn)算符)。
插入運(yùn)算符的所有化身的返回類型都是 ostream&。原型格式如下:
ostream & operator<<(type);??C++用指向字符串存儲(chǔ)位置的指針來表示字符串,指針的形式可以是char數(shù)組名,顯式的char指針或用引用括起來的字符串
char name[20] = "Duddy Diddlemore"; char *pn = "Violet D'Amore"; cout << "Hello!"; cout << name; //Duddy Diddlemore cout << pn; //Violet D'Amore??如果要獲得字符串的地址,則必須將其強(qiáng)制轉(zhuǎn)換為其他類型,如下:
char *amount = "dozen"; cout << amount; cout << (void *) amount; //prints the address of the "dozen" string17.2.1.2 拼接輸出
函數(shù)定義指出:引用將指向用于該運(yùn)算符的對(duì)象。
17.2.2 其他ostream方法
除了 operator<<() 函數(shù)外,ostream類還提供 put() 方法和 write() 方法。前者用于顯示字符,后者用于顯示字符串。
-
put()方法
- 原型如下:
cout是調(diào)用方法的對(duì)象,put()是類成員函數(shù)–該函數(shù)也返回一個(gè)指向?qū)ο蟮囊?#xff0c;所以可以用它拼接輸出:
cout.put('I').put('t'); //displaying It with two put() calls; -
write()方法
- 原型如下:
- ??注意點(diǎn):write() 方法不會(huì)在遇到空字符時(shí)自動(dòng)停止打印字符,而只是打印指定數(shù)目的字符,即使超出字符串的邊界。下面舉個(gè)例子看一下write的工作方式:
- write()方法也可用于數(shù)值數(shù)據(jù),您可以將數(shù)字的地址強(qiáng)制轉(zhuǎn)換為char*,然后傳遞給它:
都是類成員函數(shù),需要有調(diào)用對(duì)象
cout.put('w'); cout.write("Kansas",6);17.2.3 刷新輸出緩沖區(qū)
ostream類對(duì)cout對(duì)象處理的輸出進(jìn)行緩沖,所以輸出不會(huì)立即發(fā)送到目標(biāo)地址,而是被存儲(chǔ)到緩沖區(qū)中,直到緩沖區(qū)填滿。然后程序?qū)⑺⑿戮彌_區(qū),把內(nèi)容發(fā)送出去,并清空緩沖區(qū),以存儲(chǔ)新的數(shù)據(jù)。
如果實(shí)現(xiàn)不能在所希望時(shí)刷新輸出,可以使用強(qiáng)制刷新的兩個(gè)控制符之一:
- flush:刷新緩沖區(qū)
- endl:刷新緩沖區(qū),并插入一個(gè)換行符
17.2.4 用cout進(jìn)行格式化
ostream插入運(yùn)算符將值轉(zhuǎn)換為文本格式。在默認(rèn)情況下,格式化值的方式如下:
- char值:如果代表的是可打印字符,則將被作為一個(gè)字符顯示在寬度為一個(gè)字符的字段中。
- 數(shù)值整型:將以十進(jìn)制方式顯示在一個(gè)剛好容納該數(shù)字的字段中。
- 字符串:顯示在寬度等于該字符串長(zhǎng)度的字段中浮點(diǎn)類型:浮點(diǎn)類型被顯示為6位,末尾的0不顯示
- 數(shù)字以定點(diǎn)表示法顯示還是科學(xué)計(jì)數(shù)法表示,取決于值。
- 當(dāng)指數(shù)大雨6或者小于等于-5時(shí),將使用科學(xué)計(jì)數(shù)法。
- 字段寬度恰好容納數(shù)字和負(fù)號(hào)。
下面程序演示默認(rèn)的輸出情況,ps:1.0/9.0位無窮小數(shù)
#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:??該實(shí)現(xiàn)將指數(shù)變?yōu)閮晌?也有可能是三位,實(shí)現(xiàn)由不同編譯器而異),按照輸出看來,浮點(diǎn)型有效數(shù)字一般為6位。
17.2.4.1 修改顯示時(shí)使用的計(jì)數(shù)系統(tǒng)
ostream類從ios類派生而來,而ios從ios_base類派生而來。ios_base類存儲(chǔ)了描述格式狀態(tài)的信息。
通過使用 ios_base 的成員函數(shù),可以控制字段和小數(shù)位數(shù)。。因ios_base類時(shí)ostream的間接基類,可以將其方法用于ostream對(duì)象。
要控制整數(shù)以十進(jìn)制、十六進(jìn)制還是八進(jìn)制顯示,可以使用 dec、hex 和 oct 控制符。
dec(cout); // 十進(jìn)制,等價(jià)于 cout << dec; hex(cout); // 十六進(jìn)制,等價(jià)于 cout << hex; oct(cout); // 八進(jìn)制,等價(jià)于 cout << oct;使用上述設(shè)置后,程序?qū)⒁允M(jìn)制形式打印整數(shù)值,直到將格式狀態(tài)設(shè)置為其它選項(xiàng)為止。注意:控制符不是成員函數(shù),不必通過對(duì)象來調(diào)用。下面程序演示了這些控制符的用法:
#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;(十六進(jìn)制)cout << hex;cout << n << " " << n * n << "(hexadecimal)\n";//set to octal(八進(jìn)制)cout << oct;cout << n << " " << n * n << "(octal)\n";//函數(shù)調(diào)用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 調(diào)整字段寬度
由于數(shù)字的字段寬度不同,所以可以使用 width 成員函數(shù)將長(zhǎng)度不同的數(shù)字放到寬度相同的字段中。方法原型為:
int width(); // 返回字段寬度的當(dāng)前設(shè)置 int width(int i); // 將字段寬度設(shè)置為 i 個(gè)空格,并返回以前的字段寬度值width() 方法只影響將顯示的下一個(gè)項(xiàng)目,然后字段寬度將恢復(fù)為默認(rèn)值。
C++永遠(yuǎn)不會(huì)截短數(shù)據(jù),會(huì)增寬字段,以容納該數(shù)據(jù)。C/C++的原則 :顯示所有的數(shù)據(jù)比保持列的整潔更重要,C++視內(nèi)容重于形式。
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解釋:看輸出結(jié)果可知,cout.width(30)返回的是原本的值,它將字段寬度設(shè)置為30只是影響下一個(gè)要輸出第一個(gè)字符串"default field width = "(這個(gè)字符串長(zhǎng)度為22),故輸出的時(shí)候前面要填充8個(gè)空格以滿足字段寬度30。
17.2.4.3 填充字符
在默認(rèn)情況下,cout 使用空格填充字段中未被使用的部分,可以使用 fill() 成員函數(shù)來改變填充字符。
cout.fill('*');對(duì)于檢查結(jié)果,防止接收方添加數(shù)字很有用。
下面演示該成員函數(shù)的用法:
#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 設(shè)置浮點(diǎn)數(shù)的顯示精度
浮點(diǎn)數(shù)精度的含義取決于輸出模式。在默認(rèn)模式下,指的是顯示的總位數(shù)。
在定點(diǎn)模式和科學(xué)模式下,精度指的是小數(shù)點(diǎn)后的位數(shù)。
C++中的默認(rèn)精度為6位(末尾的0將不顯示),precision() 成員函數(shù)使能夠選擇其他值。
cout.precision(2); // 設(shè)置精度為2,設(shè)置后一直有效,只有重新設(shè)置會(huì)被重置下面程序演示該成員函數(shù)的用法:
#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或小數(shù)點(diǎn)
使用 setf() 函數(shù),能夠控制多種格式化特性:
cout.setf(ios_base::showpoint); // 默認(rèn)精度是6位使用默認(rèn)的浮點(diǎn)格式時(shí),會(huì)將導(dǎo)致末尾的0被顯示出來。
??showpoint是iOS_BASE類聲明中定義的類級(jí)靜態(tài)常量。類級(jí)意味著如果在成員函數(shù)定義的外面使用它,則必須在常量名前面加上作用域運(yùn)算符(:😃。因此ios_base::showpoint指的是在ios_base類定義的一個(gè)常量。
在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!輸出的第三行顯示了小數(shù)點(diǎn),因?yàn)樾?shù)點(diǎn)前面已經(jīng)包含兩位了。
17.2.4.6 setf()
ios_base類有一個(gè)受保護(hù)的數(shù)據(jù)成員,其中的各位分別控制著格式化的各個(gè)方面。
對(duì)于setf() 函數(shù),有兩個(gè)原型:
// 此為原型1 fmtflags setf(fmtflags);fmtflags 是 bitmask類型的typedef名,用于存儲(chǔ)格式標(biāo)記,該名稱是在ios_base中定義的。
其中ios_base定義了代表位置的常量,其中一些定義為:
因?yàn)槎际?ios_base類中定義,所以使用時(shí),必須加上作用域解析運(yùn)算符。
#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進(jìn)制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!??僅當(dāng)十進(jìn)制時(shí)才使用加號(hào),c++將十六進(jìn)制和八進(jìn)制都視為無符號(hào)的。
bitmask 類型是一種用來存儲(chǔ)各個(gè)位值的類型。可以是整型、枚舉,也可以是STL bitset容器。
第二種 setf() 函數(shù)的原型:
// 第一個(gè)參數(shù):包含所需設(shè)置的fmtflags值 // 第二個(gè)參數(shù):指出要清除第一個(gè)參數(shù)中的哪些位 fmtflags setf(fmtflags,fmtflags);清除位(clearing the bit):將第3位設(shè)置為1表示以10為基數(shù),將第4位設(shè)置為1表示以8為基數(shù),將第5位設(shè)置為1表示以16為基數(shù)。假設(shè)輸出是以10為基數(shù),而要將它設(shè)置為16為基數(shù),則不僅需要將第5位設(shè)置為1,還需要將第三位設(shè)置為0,這就叫作清除位。
例如,要左對(duì)齊,則使用:
ios_base::fmtflags old = cout::setf(ios::left, ios::adjustfield);下面函數(shù)調(diào)用和使用十六進(jìn)制控制符的作用相同:
cout.setf(ios_base::hex,ios_base::basefield);定點(diǎn)表示法意味著使用格式123.4來表示浮點(diǎn)值,科學(xué)表示法則意味著使用格式1.23e04.對(duì)于printf()的說明符,則可能知道,定點(diǎn)表示法對(duì)應(yīng)與%f說明符,而科學(xué)表示法對(duì)應(yīng)于%e說明符。
如果要恢復(fù)以前的設(shè)置,則使用
cout.setf(old,ios::adjustfield);在調(diào)用setf() 后可以通過unsetf() 來消除,unsetf() 的原型如下:
// mask 是位模式,mask中是所有的位都設(shè)置為1,將使得對(duì)應(yīng)的位被復(fù)位(置為0) void unsetf(fmtflags mask);其對(duì)應(yīng)的用法如下:
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++標(biāo)準(zhǔn)中,定點(diǎn)表示法和科學(xué)表示法都有兩個(gè)特征:
- 精度指的是小數(shù)位數(shù),而不是總位數(shù)
- 顯示末尾的0
下面是有關(guān)setf()的版本二的相關(guān)用法:
#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(科學(xué)技術(shù)法) 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的語句存儲(chǔ)了之前浮點(diǎn)型的輸出結(jié)果的格式,其右值對(duì)下面的語句才開始起作用,就好比如之前學(xué)的cout.width(),line26的setf以原本的顯示模式作為第一參數(shù)(目標(biāo)顯示模式),ios_base::floatfield作為第二參數(shù)。
17.2.4.7 標(biāo)準(zhǔn)控制符
17.2.4.8 頭文件iomanip
C++ 在頭文件 iomanip中提供了一些控制符。其中3個(gè)最常用的控制符分別是:
- setprecision():設(shè)置精度
- 接受一個(gè)指定精度的整數(shù)參數(shù)
- setfill():填充字符
- 接受一個(gè)指定填充字符的char參數(shù)
- setw():字符寬度
- 接受一個(gè)指定字段寬度的整數(shù)參數(shù)
下面對(duì)這些控制符進(jìn)行簡(jiǎn)單演示:
#include <iostream> #include <iomanip> #include <cmath> using namespace std; int main(){cout << fixed << right;//設(shè)置輸出方式:定點(diǎn)表示法、往右對(duì)齊// 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進(jìn)行輸入
cin對(duì)象將標(biāo)準(zhǔn)輸入表示為字節(jié)流,通常情況下,通過鍵盤來生成這種字符流。
17.3.1 cin>>如何檢查輸入
不同版本的抽取運(yùn)算符查看輸入流的方法都是相同的。他們跳過空白(空格、換行符和制表符),直到遇到非空白字符。
- 單字符模式
- >> 運(yùn)算符將讀取該字符,將它放置到指定的位置
- 其他模式
- >> 運(yùn)算符將讀取一個(gè)指定類型的數(shù)據(jù)。
對(duì)于下面的代碼:
int evolution; cin >> evolution;若向evolution中輸入-123Z,那么evolution只能取到3就截止了,因?yàn)閆字符對(duì)應(yīng)int來說不是有效的字符,故其留在了輸入流里,下個(gè)cin將會(huì)從Z的位置開始讀取。
17.3.2 流狀態(tài)
流狀態(tài)(被定義為isolate類型,而isolate是一種bitmask類型)。由3個(gè)ios_base元素組成:
- eofbit:表示到達(dá)文件末尾
- badbit:遇到無法診斷的失敗破壞流
- failbit:未能讀取到預(yù)期的字符
其中的每個(gè)元素都是一位,可以是1(設(shè)置)或0(清除)。當(dāng)3個(gè)狀態(tài)位都被設(shè)置為0時(shí),說明一切順利。
clear() 和 setstate() 類型,都是重置狀態(tài),但采取的方式不同。
- clear():將狀態(tài)設(shè)置為它的參數(shù)
- setstate():只影響其參數(shù)中設(shè)置的位置,而不會(huì)影響其他位。
exceptions() 方法返回一個(gè)位字段,包含3位,分別對(duì)應(yīng)于eofbit、failbit、badbit。修改流狀態(tài)涉及clear() 或 setstate() ,都將使用clear()。當(dāng)前狀態(tài)中的對(duì)應(yīng)位也被設(shè)置,則clear() 將引發(fā) ios_base::failure 異常。如果兩個(gè)值都設(shè)置了badbit,將發(fā)生這種情況。ios_base::failure是std::exception派生來的類,因此包含一個(gè)what()方法
exceptions() 的默認(rèn)設(shè)置為 goodbit,沒有引發(fā)異常,但重載的 exceptions(isolate) 函數(shù)使得能夠控制其行為。
cin.exceptions(badbit);位運(yùn)算符OR 使能夠指定多位。
cin.exceptions(badbit | eofbit);下面程序演示了其能夠在failbit被設(shè)置之后捕獲異常:
#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)輸入的預(yù)期數(shù)據(jù)類型錯(cuò)誤,failbit被置為1,因而導(dǎo)致了goodbit置為0,此時(shí)cin.good()–>input,導(dǎo)致了input最后的值為0而不是為2.(不同的編譯器可能不同,clion上是這樣的我認(rèn)為,本書的例子給出的output的last_input_value 為 錯(cuò)例輸入前的那個(gè)整數(shù),放在這里就是2)。
補(bǔ)充一下:為什么goodbit、badbit、eofbit、failbit分別問0、1、2、4?
用cout檢測(cè)goodbit, badbit, eofbit, failbit的值分別是0,1,2,4,這與上面的表格正好完全對(duì)應(yīng)著
(goodbit:0000 0000; badbit:0000 0001;eofbit:0000 0010; failbit:0000 0100)。
只有在流狀態(tài)的良好的情況(所有的位都被清楚),下面的測(cè)試中while的循環(huán)條件才會(huì)為true。設(shè)置流狀態(tài)位將對(duì)后面的輸入或者輸出關(guān)閉,直到位將被清除。
while (cin >> input) {sum += input; } // 可以在此處增加 clear() 來清除流狀態(tài) //cout.clear(); cin >> input; // don't work如果希望程序在流狀態(tài)位被設(shè)置后能夠讀取后面的輸入,就必須將流狀態(tài)設(shè)置為良好。可以通過調(diào)用 clear() 來實(shí)現(xiàn)。導(dǎo)致輸入循環(huán)終止的不匹配輸入仍留在輸入隊(duì)列中,程序必須跳過它。
//在cin >> input前、cout.clear()之間增加 while(!isspace(cin.get())){continue; //get rid of badinput }is space()函數(shù)是一個(gè)cctype函數(shù),它在參數(shù)是空白字符的時(shí)候返回true,這樣上面的語句能夠跳過當(dāng)前輸入錯(cuò)誤的字符or字符串。還有另一個(gè)辦法保證接下來的輸入不會(huì)出錯(cuò):丟棄行中的剩余部分。
while(cin.get() != '\n'){contiune;//get rid of line left }現(xiàn)在,假設(shè)循環(huán)是由于到達(dá)文件尾或者由于硬件故障而終止的,則處理錯(cuò)誤輸入的代碼將顯得毫無意義。可以使用fail()方法檢測(cè)是否正確。(ps:fail()在failbit或badbit其一有問題都會(huì)返回true),故需要排除后面一種情況,下面看個(gè)例子:
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) 提供不跳過空白的單個(gè)字符輸入功能
- 函數(shù) get(char*, int, char) 和 getline(char*, int, char)在默認(rèn)情況下讀取整行而不是一個(gè)單詞。
都稱為 非格式化輸入函數(shù)(unformatted input functions)。都只是讀取字符輸入,而不會(huì)跳過空白,也不進(jìn)行數(shù)據(jù)轉(zhuǎn)換。
17.3.3.1 單字符輸入
(1)成員函數(shù)get(char &):返回一個(gè)指向用于調(diào)用它的istream對(duì)象的引用
先來看一個(gè)程序:
int ct = 0; char ch; cin.get(ch); while(ch != '\n'){cout << ch;ct++;cin.get(ch); } cout << ct << endl; //輸入I C++ clearly.<enter>按下回車后,這行輸入將被發(fā)送給程序。上述程序段先讀入I,cout顯示它,并將ct遞增到1。接著,讀取I后面的空格字符,顯示它,并將ct遞增到2,這一過程將一直繼續(xù)下去,直到程序?qū)⒒剀囨I作為換行符處理,并終止循環(huán)。這里的get(ch),不僅考慮可打印字符,還考慮空格。
如果使用**>>**,那么代碼會(huì)跳過空格,這樣的話將不考慮空格,上面的例子輸出:IC++clearly.然而使用這個(gè)還有一個(gè)弊端,抽取運(yùn)算符跳過了跳過了換行符,不會(huì)將換行符賦給ch,這樣while循環(huán)成了死循環(huán)。
對(duì)于下面的例子:
char c1,c2,c3; cin.get(c1).get(c2) >> c3;首先,cin.get(c1)將第一個(gè)輸入字符賦值給c1,返回調(diào)用對(duì)象cin,接著就可以對(duì)第二個(gè)字符賦值給c2,c3的輸入語句可壓縮為cin >> c3;
如果到達(dá)了文件尾(無論是真實(shí)的,或者是鍵盤仿真的),它都不會(huì)給其參數(shù)賦值,因?yàn)榈搅宋募?#xff0c;沒有值可供給參數(shù)了。最重要的是該方法還調(diào)用setstate(failbit),導(dǎo)致cin的測(cè)試結(jié)果為false。
char ch; while (cin.get()){//process input }只要是持續(xù)存在有效輸入,那么它的返回值為都是cin,則while條件判其為true,到達(dá)文件末尾時(shí),返回值判定為false,循環(huán)終止。
(2)成員函數(shù)get(void)
get(void)成員函數(shù)還讀取空白,但使用返回值來將輸入傳遞給程序,其函數(shù)返回類型為int(或者某種更大的整型),這樣下面語句編寫的時(shí)候會(huì)非法:
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)到達(dá)文件尾后,cin.get(void)都將返回值EOF----頭文件iostream提供的一個(gè)符號(hào)常量。所以為了避免不要的錯(cuò)誤,ch需要聲明為int的類型,而非char類型,避免EOF無法用char類型來表示。
17.3.3.2 采取哪種單字符輸入形式:
對(duì)于 >>、get(char &) 或者 get(void)的選擇問題上,確定是否跳過空格,如果跳過空白則使用抽取運(yùn)算符>>。
get() 方法會(huì)檢查程序每個(gè)字符。可以將cin.get()替換所有的getchar(),用cout.put(ch)替換所有的putchar()來將c程序轉(zhuǎn)換為c++程序
17.3.3.3 字符串輸入:getline()、get()和ignore()
對(duì)于字符串的輸入成員函數(shù),getline() 成員函數(shù)和 get() 的字符串讀取版本都讀取字符串,他們的函數(shù)特征標(biāo)相同。
// 第一個(gè)參數(shù):放置輸入字符串的內(nèi)存單元的地址 // 第二個(gè)參數(shù):比要讀取的最大字符數(shù)大1(額外的一個(gè)字符用于存儲(chǔ)結(jié)尾的空字符,以便輸入存儲(chǔ)為一個(gè)字符串) // 第三個(gè)參數(shù):指定用做分界符的字符 istream & get(char *, int, char); istream & get(char *, int); istream & getline(char *, int, char); istream & getline(char *, int);下面代碼將字符輸入讀取到字符數(shù)組line中:
char line[50]; cin.get(line,50);由上面的說明可知,cin.get()函數(shù)將在到達(dá)第49個(gè)字符或者遇到換行符后停止將輸入讀取到數(shù)組中。
對(duì)于get() 和 getline() 方法的區(qū)別:
- get():將換行符留在輸入流中
- getline():抽取并丟棄輸入流中的換行符。
特殊的成員函數(shù) ignore(),函數(shù)原型表示:
istream & ignore(int = 1,int = EOF); // 原型中的兩個(gè)參數(shù)提供的默認(rèn)值為 1 和 EOF。默認(rèn)參數(shù)值EOF導(dǎo)致 ignore() 讀取指定數(shù)目的字符或讀取文件尾。
下面程序演示getline()、get()的使用方法,還順便介紹一個(gè)新的函數(shù)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個(gè)字符或直到到達(dá)第一個(gè)換行符(針對(duì)的是輸入流中殘存的字符流)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 意外字符串輸入
兩種特殊情況:無輸入以及輸入到達(dá)或超過函數(shù)調(diào)用指定的最大字符數(shù)。
對(duì)于get(char *,int)和getline(),如果不能抽取字符,它們將把空值字符放置到輸入串中,并使用setstate()設(shè)置為failbit。出現(xiàn)這種情況有兩種可能:
-
輸入方法立刻到達(dá)了文件尾
-
對(duì)于get(char *,int)來說,另一種可能是輸入了空行。
- char temp[80]; while (cin.get(temp,80)){ //terminates on empty line... }
-
getline()遇到空行不會(huì)設(shè)置failbit,這是因?yàn)間etline()仍將抽取換行符,如果希望getline()遇到空行時(shí)就終止循環(huán)可以這樣寫:
- char tmep[80]; while(cin.getline(temp,80) && temp[0] != '\0') //terminates on empty line
-
getline()如果讀取0-79個(gè)字符,并且下一個(gè)字符不是換行符,則設(shè)置failbit,后續(xù)的字符無法輸入進(jìn)來。
17.3.4 其他istream方法
- read():讀取指定數(shù)目的字節(jié),并將其存儲(chǔ)在指定的位置中。不會(huì)在輸入后加上空值字符。(返回類型為istream&)
- peek():返回輸入中的下一個(gè)字符,但不抽取輸入流中的字符。
- gcount():返回最后一個(gè)非格式化抽取方法讀取的字符數(shù)。(strlen比它快)
- putback():將一個(gè)字符插入到輸入字符串中,被插入的字符將是下一條輸入語句讀取的第一個(gè)字符。(返回類型為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()來確定是否讀取了整行。如果一行中只有部分內(nèi)容被加入到數(shù)組中,程序?qū)h除余下內(nèi)容:
#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(截?cái)? 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(截?cái)? your title.Name: Ella Fish Title: Executive17.4. 文件輸入和輸出
C++ I/O類軟件包處理文件輸入和輸出的方式與處理標(biāo)準(zhǔn)輸入和輸出的方式相似。
- 要寫入文件,需要?jiǎng)?chuàng)建一個(gè)ofstream對(duì)象,并使用ofstream方法。
- 要讀取文件,需要?jiǎng)?chuàng)建一個(gè)ifstream對(duì)象,并使用ifstream方法。
17.4.1 簡(jiǎn)單的文件I/O
要讓程序?qū)懭胛募淖龇?#xff1a;
- 創(chuàng)建一個(gè)ofstream對(duì)象來管理輸出流
- 將該對(duì)象與特定的文件關(guān)聯(lián)起來
- 以使用cout的方式使用該對(duì)象,唯一的區(qū)別是輸出將進(jìn)入文件,而不是屏幕。
讀取文件的做法:
- 創(chuàng)建一個(gè)ifstream對(duì)象來管理輸入流
- 將該對(duì)象與特定的文件關(guān)聯(lián)起來
- 以使用cin的方式使用該對(duì)象
當(dāng)輸入和輸出流對(duì)象過期(如程序終止)時(shí),到文件的連接將自動(dòng)關(guān)閉,另外,也可以使用 close() 方法來顯式地關(guān)閉到文件的連接。關(guān)閉連接并不會(huì)刪除流,只是將流重新連接到同一個(gè)文件或者另一個(gè)文件。
ifstream fin; ofstream fout; fout.close(); fin.close();下面看一個(gè)簡(jiǎn)單的例子:輸入文件名,并以該輸入的文件,將一些信息寫入到該文件中,然后關(guān)閉該文件。
#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; }下面在目錄下找到了相應(yīng)的文件:
17.4.2 流狀態(tài)檢查和is_open()
C++文件流從 ios_base 類那里繼承了一個(gè)流狀態(tài)成員。在C++通過使用 is_open() 方法 來檢查文件是否被成功打開。
假如打開一個(gè)不存在的文件進(jìn)行輸入時(shí),將設(shè)置為failbit位,因此可以這樣寫:
fin.open("fileName"); if(fin.fail()){...} //or if(!fin){...} //or if(!fin.open()){...}17.4.3 命令行處理技術(shù)
文件處理程序通常是使用命令行參數(shù)來指定文件。命令行參數(shù)是用戶在輸入命令時(shí),在命令行中輸入的參數(shù)。
//wc是程序名,后面三個(gè)參數(shù)是作為命令行參數(shù)傳遞給程序的文件名 wc report1 report2 report3C++中在命令行環(huán)境中運(yùn)行的程序能夠訪問命令行參數(shù)的機(jī)制
// argc:命令行中的參數(shù)個(gè)數(shù) // argv:變量為一個(gè)指針,指向一個(gè)指向char的指針。argv[0] 表示指向一個(gè)參數(shù)(字符串指針) int main(int argc, char *argv[])可以在命令行中打開main.cpp:
然后在終端進(jìn)行命令行輸入:
g++ main.cpp //編譯,會(huì)產(chǎn)生一個(gè)a.out文件 ./a.out article1(這是文件)輸出: 43842 characters in ./a.out 3 characters in article1(存有一個(gè)漢字) 43845 characters in all files argc = 2補(bǔ)充:不同的編碼方式,漢字所占的字符是不同的,在國家標(biāo)準(zhǔn)GB2312中:一個(gè)漢字 = 2 個(gè)字符;在UTF-8中,一個(gè)漢字 = 3個(gè)字符。
17.4.4 文件模式
將流與文件關(guān)聯(lián)時(shí),都可以指定文件模式的第二個(gè)參數(shù)
ifstream fin("banjo",mode1); ofstream fout(); fout.open("harp",mode2);文件模式描述的是文件將被如何使用:讀、寫、追加等。
ios_base類定義了一個(gè) openmode 類型,用于表示模式。
位運(yùn)算符OR(|):將兩個(gè)位值合并成一個(gè)可用于設(shè)置兩個(gè)位的值。
ofstream fout("bageis",ios_base::out | ios_base::app);在 ios_base::out 本身將導(dǎo)致文件將被截短,但與 ios_base::in 一起使用時(shí),不會(huì)導(dǎo)致文件被截短,沒有列出的組合。
ios_base::ate 和 ios_base::app 都將文件指針指向打開的文件結(jié)尾。區(qū)別之處:
- ios_base::app:將指針放到文件尾
- ios_base::ate:只允許數(shù)據(jù)添加到文件尾。
下面是一些C++和C的打開模式
1、文件追加
下面程序演示在文件尾追加數(shù)據(jù):
#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.二次執(zhí)行: 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、二進(jìn)制文件
數(shù)據(jù)存儲(chǔ)在文件中,是兩種形式:
- 文本格式:將所有的內(nèi)容都存儲(chǔ)為文本。
- 二進(jìn)制格式:將存儲(chǔ)值的計(jì)算機(jī)內(nèi)部表示。
- 計(jì)算機(jī)不是存儲(chǔ)字符,而是以二進(jìn)制的形式存儲(chǔ)。
文件兩種存儲(chǔ)形式的優(yōu)缺點(diǎn)
- 文本格式
- 優(yōu)點(diǎn)
- 便于讀取
- 使用編輯器或字符處理器來讀取和編輯
- 優(yōu)點(diǎn)
- 二進(jìn)制格式
- 優(yōu)點(diǎn)
- 不會(huì)有轉(zhuǎn)換誤差或者舍入誤差
- 保存速度快,不需要轉(zhuǎn)換
- 可以大塊存儲(chǔ)數(shù)據(jù)。
- 數(shù)據(jù)特征不同,空間占用小
- 缺點(diǎn)
- 換系統(tǒng)或者OS會(huì)導(dǎo)致無法正常使用
- 優(yōu)點(diǎn)
要以二進(jìn)制格式(而不是文本格式)存儲(chǔ)數(shù)據(jù),可以使用 write() 成員函數(shù)。
將文件使用二進(jìn)制的格式:
fout.write((char *) &pl, sizeof pl) // 將pl地址強(qiáng)制轉(zhuǎn)換為指向char的指針 // sizeof() 獲取字節(jié)數(shù) // 這條語句導(dǎo)致程序前往pl的地址,并將開始的36個(gè)字節(jié)復(fù)制到與fout相關(guān)聯(lián)的文件。要使用文件恢復(fù)信息,則使用通過 ifstream對(duì)象 使用相應(yīng)的 read() 方法:
ifstream fin("planets.dat",ios_base::in | ios_base::binary); fin.read((char *) &pl,sizeof pl); //這將從文件中復(fù)制sizeof pl個(gè)字節(jié)到pl結(jié)構(gòu)中read() 和 write() 成員函數(shù)的功能是相反,一般使用 read() 來恢復(fù) write() 寫入的數(shù)據(jù)。
下面程序使用以上方法創(chuàng)建和讀取二進(jìn)制文件
#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;}//首次執(zhí)行: 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//第二次執(zhí)行 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來替代字符數(shù)組來表示planet中的name。在不做出比較大的修改時(shí)答案是否定的。因?yàn)閟tring本身是沒有字符串的,而是包含一個(gè)指向存儲(chǔ)字符串的內(nèi)存單元的指針,故復(fù)制的時(shí)候,只會(huì)復(fù)制字符串的地址而不是字符串的數(shù)據(jù)。
17.4.5 隨機(jī)存取
隨機(jī)存取指的是直接移動(dòng)(不是依次移動(dòng))到文件的任何位置。
隨機(jī)存取常被用于數(shù)據(jù)庫文件,程序維護(hù)一個(gè)獨(dú)立的索引文件。文件指出數(shù)據(jù)在主數(shù)據(jù)文件中的位置。
需要在一種文件中移動(dòng)的方式,ifstream類繼承的兩個(gè)方法:
-
hiseekg():將輸入指針移到指定的文件位置。
- 可用于 ifstream 對(duì)象
-
seekp():將輸出指針移到指定的xing 文件位置。
- 可用于 ofstream對(duì)象
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); // 定位到離第二個(gè)參數(shù)指定的文件位置特定距離(單位為字節(jié))的位置 istream & seekg(streampos); // 定位到離開文件開頭特定距離(單位為字節(jié))的位置下面是一些關(guān)于原型1調(diào)用的示例:
ps:常量ios_base::cur指相對(duì)于當(dāng)前位置的偏移量;ios_base::end指相對(duì)于文件尾的偏移量。
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下面是一些關(guān)于原型2調(diào)用的示例:
fin.seekg(112); //第113個(gè)字節(jié)下面程序?qū)?7.4.4進(jìn)行優(yōu)化,所用的流既可以輸出也可以輸入,該程序的功能為修改特定位置的二進(jìn)制數(shù)據(jù):
#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 內(nèi)核格式化
讀取string對(duì)象中的格式化信息或?qū)⒏袷交畔懭雜tring對(duì)象中被稱為內(nèi)核格式化。
頭文件 sstream 定義了一個(gè)從 ostream類派生來的ostringstream類。如果創(chuàng)建了一個(gè)ostringstream對(duì)象,則可以將信息寫入其中,并將其信息存儲(chǔ)。
istringstream 和 ostringstream類使得能夠使用 istream 和 ostream 類的方法來管理存儲(chǔ)在字符串中的字符數(shù)據(jù)。
下面是一個(gè)有關(guān)內(nèi)核格式化的簡(jiǎn)短示例:
#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()方法可以“凍結(jié)”該對(duì)象,使得信息不能寫入該對(duì)象中
下面使用重載的>>運(yùn)算符讀取字符串中的內(nèi)容,每次讀取一個(gè)單詞:
#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總結(jié)
以上是生活随笔為你收集整理的c++ primer plus 第 17 章 输入、输出和文件的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 构建数据科学档案:机器学习项目
- 下一篇: 数据科学家如何找到心仪的工作?