




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、第十一章 流類庫與輸入/輸出目錄11.1 I/O流的概念及流類庫結(jié)構(gòu)11.2 輸出流11.3 輸入流11.4 輸入/輸出流11.5 綜合實例對個人銀行賬戶管理程序的改進11.6 深度探索11.7 小結(jié)211.1 I/O流的概念及流類庫結(jié)構(gòu)當程序與外界環(huán)境進行信息交換時,存在著兩個對象,一個是程序中的對象,另一個是文件對象。流是一種抽象,它負責在數(shù)據(jù)的生產(chǎn)者和數(shù)據(jù)的消費者之間建立聯(lián)系,并管理數(shù)據(jù)的流動。程序建立一個流對象,并指定這個流對象與某個文件對象建立連接,程序操作流對象,流對象通過文件系統(tǒng)對所連接的文件對象產(chǎn)生作用。讀操作在流數(shù)據(jù)抽象中被稱為(從流中)提取,寫操作被稱為(向流中)插入。3流
2、類庫結(jié)構(gòu)411.1 I/O流的概念及流類庫結(jié)構(gòu)11.2 輸出流最重要的三個輸出流是ostreamofstreamostringstream511.2.1 構(gòu)造輸出流對象預(yù)先定義的輸出流對象:cout 標準輸出cerr 標準錯誤輸出,沒有緩沖,發(fā)送給它的內(nèi)容立即被輸出。clog 類似于cerr,但是有緩沖,緩沖區(qū)滿時被輸出。611.2 輸出流11.2.1 構(gòu)造輸出流對象(續(xù))ofstream類支持磁盤文件輸出如果在構(gòu)造函數(shù)中指定一個文件名,當構(gòu)造這個文件時該文件是自動打開的ofstream myFile(filename);可以在調(diào)用默認構(gòu)造函數(shù)之后使用open成員函數(shù)打開文件ofstream
3、myFile; /聲明一個靜態(tài)文件輸出流對象myFile.open(filename); /打開文件,使流對象與文件建立聯(lián)系在構(gòu)造對象或用open打開文件時可以指定模式ofstream myFile(filename, ios_base:out | ios_base:binary);711.2 輸出流11.2.2使用插入運算符和操縱符(manipulator)插入()運算符是所有標準C+數(shù)據(jù)類型預(yù)先設(shè)計的。用于傳送字節(jié)到一個輸出流對象??刂戚敵鰧挾葹榱苏{(diào)整輸出,可以通過在流中放入setw操縱符或調(diào)用width成員函數(shù)為每個項指定輸出寬度。dec、oct和hex操縱符設(shè)置輸入和輸出的默認進制。8
4、11.2 輸出流例11-1 使用width控制輸出寬度#include using namespace std;int main() double values = 1.23, 35.36, 653.7, 4358.24 ;for(int i = 0; i 4; i+) cout.width(10);cout valuesi endl;return 0;911.2 輸出流 11.2.2使用插入運算符和操縱符(manipulator)輸出結(jié)果: 1.23 35.36 653.7 4358.24例11-2使用setw操縱符指定寬度/11_2.cpp#include #include #includ
5、e using namespace std;int main() double values = 1.23, 35.36, 653.7, 4358.24 ;string names = Zoot, Jimmy, Al, Stan ;for (int i = 0; i 4; i+)cout setw(6) namesi setw(10) valuesi endl;return 0;1011.2 輸出流 11.2.2使用插入運算符和操縱符(manipulator)輸出結(jié)果: 1.23 35.36 653.7 4358.24例11-3設(shè)置對齊方式/11_3.cpp#include #include
6、#include using namespace std;int main() double values = 1.23, 35.36, 653.7, 4358.24 ;string names = Zoot, Jimmy, Al, Stan ;for (int i=0;i4;i+)cout setiosflags(ios_base:left) setw(6) namesi resetiosflags(ios_base:left) setw(10) valuesi endl;return 0;1111.2 輸出流 11.2.2使用插入運算符和操縱符(manipulator)輸出結(jié)果:Zoot
7、1.23Jimmy 35.36Al 653.7Stan 4358.24例11-4控制輸出精度/11_4.cpp#include #include #include using namespace std;int main() double values = 1.23, 35.36, 653.7, 4358.24 ;string names = Zoot, Jimmy, Al, Stan ;for (int i=0;i4;i+)cout setiosflags(ios_base:left) setw(6) namesi resetiosflags(ios_base:left) setw(10)
8、setprecision(1) valuesi endl;return 0;1211.2 輸出流 11.2.2使用插入運算符和操縱符(manipulator)輸出結(jié)果:Zoot 1Jimmy 4e+001Al 7e+002Stan 4e+00311.2.3文件輸出流成員函數(shù)輸出流成員函數(shù)有三種類型:與操縱符等價的成員函數(shù)。執(zhí)行非格式化寫操作的成員函數(shù)。其它修改流狀態(tài)且不同于操縱符或插入運算符的成員函數(shù)。1311.2 輸出流11.2.3文件輸出流成員函數(shù)(續(xù))open函數(shù)把流與一個特定的磁盤文件關(guān)聯(lián)起來。需要指定打開模式。put函數(shù)把一個字符寫到輸出流中。write函數(shù)把內(nèi)存中的一塊內(nèi)容寫到一個
9、文件輸出流中seekp和tellp函數(shù)操作文件流的內(nèi)部指針close函數(shù)關(guān)閉與一個文件輸出流關(guān)聯(lián)的磁盤文件錯誤處理函數(shù)在寫到一個流時進行錯誤處理1411.2 輸出流例11-5 向文件輸出/11_5.cpp#include using namespace std;struct Date int mondy, day, year; ;int main() Date dt = 6, 10, 92 ;ofstream file(date.dat, ios_base:binary);file.write(reinterpret_cast(&dt),sizeof(dt);file.close();retu
10、rn 0;1511.2 輸出流 11.2.3 文件輸出流成員函數(shù)11.2.4 二進制輸出文件以通常方式構(gòu)造一個流,然后使用setmode成員函數(shù),在文件打開后改變模式。使用ofstream構(gòu)造函數(shù)中的模式參量指定二進制輸出模式1611.2 輸出流11.2.5 字符串輸出流( ostringstream )用于構(gòu)造字符串功能支持ofstream類的除open、close外的所有操作str函數(shù)可以返回當前已構(gòu)造的字符串典型應(yīng)用將數(shù)值轉(zhuǎn)換為字符串1711.2 輸出流例11-6用ostringstream將數(shù)值轉(zhuǎn)換為字符串/11_6.cpp#include #include #include usin
11、g namespace std;template inline string toString(const T &v) ostringstream os;/創(chuàng)建字符串輸出流os v;/將變量v的值寫入字符串流return os.str();/返回輸出流生成的字符串int main() string str1 = toString(5);cout str1 endl;string str2 = toString(1.2);cout str2 endl;return 0;1811.2 輸出流 11.2.5 字符串輸出流( ostringstream )輸出結(jié)果:51.2函數(shù)模板toString可以
12、把各種支持“)對于所有標準C+數(shù)據(jù)類型都是預(yù)先設(shè)計好的。是從一個輸入流對象獲取字節(jié)最容易的方法。ios類中的很多操縱符都可以應(yīng)用于輸入流。但是只有少數(shù)幾個對輸入流對象具有實際影響,其中最重要的是進制操縱符dec、oct和hex。2111.3 輸入流11.3.4輸入流相關(guān)函數(shù)open函數(shù)把該流與一個特定磁盤文件相關(guān)聯(lián)。get函數(shù)的功能與提取運算符()很相像,主要的不同點是get函數(shù)在讀入數(shù)據(jù)時包括空白字符。(第6章介紹過)getline的功能是從輸入流中讀取多個字符,并且允許指定輸入終止字符,讀取完成后,從讀取的內(nèi)容中刪除終止字符。(第6章介紹過)read成員函數(shù)從一個文件讀字節(jié)到一個指定的內(nèi)存
13、區(qū)域,由長度參數(shù)確定要讀的字節(jié)數(shù)。如果給出長度參數(shù),當遇到文件結(jié)束或者在文本模式文件中遇到文件結(jié)束標記字符時結(jié)束讀取。2211.3 輸入流11.3.4輸入流相關(guān)函數(shù)(續(xù))seekg函數(shù)用來設(shè)置文件輸入流中讀取數(shù)據(jù)位置的指針。tellg函數(shù)返回當前文件讀指針的位置。close函數(shù)關(guān)閉與一個文件輸入流關(guān)聯(lián)的磁盤文件。2311.3 輸入流例11-7 get函數(shù)應(yīng)用舉例/11_7.cpp#include using namespace std;int main() char ch;while (ch = cin.get() != EOF)cout.put(ch);return 0;2411.3 輸入流
14、 11.3.4 輸入流相關(guān)函數(shù)例11-8為輸入流指定一個終止字符:/11_8.cpp#include #include using namespace std;int main() string line; cout Type a line terminated by t endl; getline(cin, line, t); cout line endl;return 0;2511.3 輸入流 11.3.4 輸入流相關(guān)函數(shù)例11-9從一個payroll文件讀一個二進制記錄到一個結(jié)構(gòu)中/11_9.cpp#include #include #include using namespace st
15、d;struct SalaryInfo unsigned id;double salary;int main() SalaryInfo employee1 = 600001, 8000 ;ofstream os(payroll, ios_base:out | ios_base:binary);os.write(reinterpret_cast(&employee1), sizeof(employee1);os.close();2611.3 輸入流 11.3.4 輸入流相關(guān)函數(shù)例11-9(續(xù))ifstream is(payroll, ios_base:in | ios_base:binary);
16、if (is) SalaryInfo employee2;is.read(reinterpret_cast(&employee2), sizeof(employee2);cout employee2.id employee2.salary endl; else cout ERROR: Cannot open file payroll. endl;is.close();return 0;2711.3 輸入流 11.3.4 輸入流相關(guān)函數(shù)例11-10用seekg函數(shù)設(shè)置位置指針/11_10.cpp, 頭部分省略int main() int values = 3, 7, 0, 5, 4 ;ofstr
17、eam os(integers, ios_base:out | ios_base:binary);os.write(reinterpret_cast(values), sizeof(values);os.close();ifstream is(integers, ios_base:in | ios_base:binary);if (is) is.seekg(3 * sizeof(int);int v;is.read(reinterpret_cast(&v), sizeof(int);cout The 4th integer in the file integers is v endl; els
18、e cout ERROR: Cannot open file integers. endl;return 0;2811.3 輸入流 11.3.4 輸入流相關(guān)函數(shù)例11-11讀一個文件并顯示出其中0元素的位置/11_11.cpp, 頭部分省略int main() ifstream file(integers, ios_base:in | ios_base:binary);if (file) while (file) streampos here = file.tellg(); int v; file.read(reinterpret_cast(&v), sizeof(int); if (file
19、 & v = 0) cout Position here is 0 endl; else cout ERROR: Cannot open file integers. endl;file.close();return 0;2911.3 輸入流 11.3.4 輸入流相關(guān)函數(shù)11.3.5 字符串輸入流( istringstream)用于從字符串讀取數(shù)據(jù)在構(gòu)造函數(shù)中設(shè)置要讀取的字符串功能支持ifstream類的除open、close外的所有操作典型應(yīng)用將字符串轉(zhuǎn)換為數(shù)值3011.3 輸入流例11-12用ostringstream將字符串轉(zhuǎn)換為數(shù)值/11_12.cpp, 頭部分省略template i
20、nline T fromString(const string &str) istringstream is(str);/創(chuàng)建字符串輸入流T v;is v;/從字符串輸入流中讀取變量vreturn v;/返回變量vint main() int v1 = fromString(5);cout v1 endl;double v2 = fromString(1.2);cout v2 ”、“”運算符,對賬戶信息重載“”運算符。33例11-13 個人銀行賬戶管理程序程序分為6個文件: date.h是日期類頭文件date.cpp是日期類實現(xiàn)文件accumulator.h為按日將數(shù)值累加的Accumulat
21、or類的頭文件account.h是各個儲蓄賬戶類定義頭文件account.cpp是各個儲蓄賬戶類實現(xiàn)文件11_13.cpp是主函數(shù)文件。3411.5綜合實例對個人銀行賬戶管理程序的改進/date.h#ifndef _DATE_H_#define _DATE_H_#include class Date /日期類/Date類中不再有靜態(tài)成員函數(shù)read,其它內(nèi)容與例10-24完全相同,;std:istream & operator (std:istream &in, Date &date);std:ostream & operator (istream &in, Date &date) int y
22、ear, month, day;char c1, c2;in year c1 month c2 day;date = Date(year, month, day);return in;ostream & operator (ostream &out, const Date &date) out date.getYear() - date.getMonth() - date.getDay();return out;3511.5綜合實例對個人銀行賬戶管理程序的改進例11-13(續(xù))/account.h#ifndef _ACCOUNT_H_#define _ACCOUNT_H_#include da
23、te.h#include accumulator.h#include #include #include class Account /賬戶類/Account類中為以下函數(shù)增加了一個參數(shù),其它成員與例10-24完全相同virtual void show(std:ostream &out) const;inline std:ostream & operator (std:ostream &out, const Account &account) account.show(out);return out;class CreditAccount : public Account /信用賬戶類/ Cr
24、editAccount類中為以下函數(shù)增加了一個參數(shù),其它成員與例10-24完全相同virtual void show(std:ostream &out) const;/account.h中其它類的定義與例10-24完全一樣#endif /_ACCOUNT_H_3611.5綜合實例對個人銀行賬戶管理程序的改進例11-13(續(xù))/account.cpp中,只有以下幾個成員函數(shù)的實現(xiàn)有所改變,其它內(nèi)容與例10-24完全相同void AccountRecord:show() const cout date t# getId() t amount t balance t desc endl;Accoun
25、t:Account(const Date &date, const string &id) : id(id), balance(0) cout date t# id created endl;void Account:show(ostream &out) const out id tBalance: balance;void CreditAccount:show(ostream &out) const Account:show(out);out tAvailable credit: getAvailableCredit();3711.5綜合實例對個人銀行賬戶管理程序的改進例11-13(續(xù))/1
26、1_13.cpp#include account.h#include #include #include #include #include #include using namespace std;struct deleter template void operator () (T* p) delete p; ;class Controller /控制器,用來儲存賬戶列表和處理命令private:Date date;/當前日期vector accounts;/賬戶列表bool end;/用戶是否輸入了退出命令public:Controller(const Date &date) : dat
27、e(date), end(false) Controller();const Date &getDate() const return date; bool isEnd() const return end; /執(zhí)行一條命名,返回該命令是否改變了當前狀態(tài)(即是否需要保存該命令)bool runCommand(const string &cmdLine);3811.5綜合實例對個人銀行賬戶管理程序的改進例11-13(續(xù))Controller:Controller() for_each(accounts.begin(), accounts.end(), deleter();bool Control
28、ler:runCommand(const string &cmdLine) istringstream str(cmdLine);char cmd, type;int index, day;double amount, credit, rate, fee;string id, desc;Account* account;Date date1, date2;str cmd;switch (cmd) case a:/增加賬戶/ 對a(增加賬戶)、d(存款)、w(取款)、s(查詢賬戶信息)、c(改變?nèi)掌冢? n(進入下個月)的處理,與例10-24基本相同,只是把讀入?yún)?shù)的輸入流由cin改為了/ s
29、tr,并且在每種情況后直接return,其中對s命令返回false,其它皆返回true。case q:/查詢一段時間內(nèi)的賬目str date1 date2;Account:query(date1, date2);return false;case e:/退出end = true;return false;cout Inavlid command: cmdLine endl;return false;3911.5綜合實例對個人銀行賬戶管理程序的改進例11-13(續(xù))int main() Date date(2008, 11, 1);/起始日期Controller controller(date)
30、;string cmdLine;const char *FILE_NAME = commands.txt;ifstream fileIn(FILE_NAME);/以讀模式打開文件if (fileIn) /如果正常打開,就執(zhí)行文件中的每一條命令while (getline(fileIn, cmdLine)controller.runCommand(cmdLine);fileIn.close();/關(guān)閉文件ofstream fileOut(FILE_NAME, ios_base:app);/以追加模式打開文件cout (a)add account (d)deposit (w)withdraw (s
31、)show (c)change day (n)next month (q)query (e)exit endl;while (!controller.isEnd() /從標準輸入讀入命令并執(zhí)行,直到退出cout controller.getDate() tTotal: Account:getTotal() ;string cmdLine;getline(cin, cmdLine);if (controller.runCommand(cmdLine)fileOut cmdLine endl;/將命令寫入文件return 0;4011.5綜合實例對個人銀行賬戶管理程序的改進例11-13(續(xù))第一次
32、運行結(jié)果. (輸入和輸出與例9-16給出的完全相同)第二次運行結(jié)果2008-11-1 #S3755217 created2008-11-1 #02342342 created2008-11-1 #C5392394 created2008-11-5 #S3755217 5000 5000 salary2008-11-15 #C5392394 -2000 -2000 buy a cell2008-11-25 #02342342 10000 10000 sell stock 03232008-12-1 #C5392394 -16 -2016 interest2008-12-1 #C5392394 2
33、016 0 repay the credit2008-12-5 #S3755217 5500 10500 salary2009-1-1 #S3755217 17.77 10517.8 interest2009-1-1 #02342342 15.16 10015.2 interest2009-1-1 #C5392394 -50 -50 annual fee(a)add account (d)deposit (w)withdraw (s)show (c)change day (n)next month (q)query (e)exit(后面省略)第二次執(zhí)行程序時,第一次輸入的數(shù)據(jù)完全被恢復了411
34、1.5綜合實例對個人銀行賬戶管理程序的改進例11-13(續(xù))11.6.1 寬字符、寬字符串與寬流普通字符和字符串的缺陷一個漢字被拆成兩個字符例:string s = “這是一個中文字符串”;s.size():返回18s.substr(3,2):得到的結(jié)果是“且”s.find(“且”):返回34211.6 深度探索寬字符與寬字符串寬字符:wchar_t類型一般占2個字節(jié),可以直接存下一個漢字寬字符的文字以“L”開頭,例:wchar_t c = L人;寬字符串:wstring類型與string同源typedef basic_string string;typedef basic_string ws
35、tring;例wstring s = L這是一個中文字符串;s.size():返回94311.6 深度探索 11.6.1 寬字符、寬字符串與寬流寬流寬流:以寬字符為基本單位的流wistream、wifstream、wistringstream、wostream、wofstream、wostringstream、wioswcin、wcout、wcerr、wclog寬字符和寬字符串需要通過寬流輸入輸出寬流與普通流一一對應(yīng),彼此同源typedef basic_ifstream ifstream;typedef basic_ifstream wifstream;4411.6 深度探索 11.6.1 寬
36、字符、寬字符串與寬流為寬文件流配置編碼方案文件以字節(jié)為單位,編碼方案決定了寬字符和字節(jié)的對應(yīng)關(guān)系例:L“ABCD”占4個字節(jié),L“甲乙丙丁”占8個字節(jié),這由編碼方案體現(xiàn)配置方法:用“代碼頁”編號構(gòu)造locale對象執(zhí)行流的imbue成員函數(shù)示例locale loc(.936); /創(chuàng)建本地化配置方案對象wcout.imbue(loc); /設(shè)置wcout對象的編碼方案wcout L這是一個中文字符串 endl;/輸出字符串4511.6 深度探索 11.6.1 寬字符、寬字符串與寬流例11-14 用文件寬輸入流查找文件中的“人”字/11_14.cpp,部分頭文件引用省略#include usin
37、g namespace std;int main() locale loc(.936);/創(chuàng)建本地化配置方案wcout.imbue(loc);/為wcout設(shè)置編碼方案wifstream in(article.txt);/創(chuàng)建文件寬輸入流,打開文件article.txtin.imbue(loc);/為in設(shè)置編碼方案wstring line;/用來存儲一行內(nèi)容unsigned number = 0;/記錄行號while (getline(in, line) number+;/行號加1if (line.find_first_of(L人) != wstring:npos)/查找“人”字 wcout number L: line endl;/輸出包含“人”字的行return 0;4611.6 深度探索 11.6.1 寬字符、寬字符串與寬流11.6.2 對象的串行化串行化:將對象寫入文件,使得在適當?shù)臅r候?qū)ο竽軓奈募凶x出并恢復直接用write將對象內(nèi)容輸出、用read將對象恢復的問題對象中存在指針時,指針所指對象內(nèi)容不會被保存;對象的成員本身可能是存在指針的對象;對象不僅是數(shù)據(jù)的集合,還包括一系列行為,用read只能恢復數(shù)據(jù),不能觸發(fā)相應(yīng)行為4711
溫馨提示
- 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)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 民辦萬博科技職業(yè)學院《水工建筑物》2023-2024學年第二學期期末試卷
- 2025海南省安全員B證考試題庫附答案
- 武漢設(shè)計工程學院《火電廠煙氣凈化Ⅰ》2023-2024學年第二學期期末試卷
- 湖北科技學院《生物技術(shù)制藥B》2023-2024學年第二學期期末試卷
- 四川科技職業(yè)學院《房屋建筑與實務(wù)》2023-2024學年第二學期期末試卷
- 荊門職業(yè)學院《雷達信號分析》2023-2024學年第二學期期末試卷
- 黑龍江三江美術(shù)職業(yè)學院《BIM技術(shù)與軟件應(yīng)用》2023-2024學年第二學期期末試卷
- 長春早期教育職業(yè)學院《紡織品實驗與設(shè)計》2023-2024學年第二學期期末試卷
- 2024-2025學年河北省部分重點中學高三上學期12月聯(lián)考歷史試卷
- 廣西民族師范學院《連鎖經(jīng)營管理》2023-2024學年第二學期期末試卷
- 重慶市渝北區(qū)龍山小學-25版四年級寒假特色作業(yè)【課件】
- 煤礦重大災(zāi)害治理中長期規(guī)劃(防治煤塵爆炸、火災(zāi)事故)
- 2024年事業(yè)單位考試(綜合管理類A類)綜合應(yīng)用能力試題及解答參考
- 管理ABC-干嘉偉(美團網(wǎng)COO)
- 高壓氧科工作總結(jié)高壓氧科個人年終總結(jié).doc
- 《政治學概論》教學大綱
- 橋梁缺陷與預(yù)防
- 食品生物化學習題謝達平(動態(tài))
- 保安員工入職登記表
- 睿達RDCAM激光雕刻切割軟件V5.0操作說明書
- 機械設(shè)計基礎(chǔ)平面連桿機構(gòu)課件
評論
0/150
提交評論