第八﹑九章 I/O系統

Similar documents
運算子多載 Operator Overloading

新版 明解C++入門編

2013 C 1 # include <stdio.h> 2 int main ( void ) 3 { 4 int cases, a, b, i; 5 scanf ("%d", & cases ); 6 for (i = 0;i < cases ;i ++) 7 { 8 scanf ("%d %d

C/C++语言 - C/C++数据

PowerPoint Presentation

Strings

c_cpp

untitled

C 1 # include <stdio.h> 2 int main ( void ) { 4 int cases, i; 5 long long a, b; 6 scanf ("%d", & cases ); 7 for (i = 0;i < cases ;i ++) 8 { 9

Microsoft PowerPoint - ch12.ppt

FY.DOC

Microsoft PowerPoint - CPP-Ch Print.ppt [兼容模式]

Microsoft PowerPoint - Class5.pptx

第3章.doc

untitled

电邮.FIT)

!"# $%& %!"# $%& %!"#$%& %! ( )***%% ) $)! +**+),,* -)+.* )( ) +, +*.*)+..**! )$,*)+$))$!"!#

C++ 程序设计 OJ9 - 参考答案 MASTER 2019 年 6 月 7 日 1

ebook55-13

Microsoft PowerPoint - string_kruse [兼容模式]

CC213

Chapter12 Derived Classes

Microsoft Word - 把时间当作朋友(2011第3版)3.0.b.06.doc

untitled

C 1

untitled

1 Project New Project 1 2 Windows 1 3 N C test Windows uv2 KEIL uvision2 1 2 New Project Ateml AT89C AT89C51 3 KEIL Demo C C File

ebook39-5

迅速在两个含有大量数据的文件中寻找相同的数据

51 C 51 isp 10 C PCB C C C C KEIL

untitled

Microsoft Word - CPE考生使用手冊 docx

e bug 0 x=0 y=5/x 0 Return 4 2

105A 資管一程式設計實驗 06 函式定義謝明哲老師 2 程式設計實驗 6.3: 自行定義一個可以接受兩個整數並傳回其最大公因數的函式, 接著利用該函式自 行定義一個可以接受兩個整數並傳回其最小公倍數函式 // gcd_fcn.cpp int gcd(int m,

NethersoleJO89(8).indd

全国计算机技术与软件专业技术资格(水平)考试

untitled

《米开朗琪罗传》

Microsoft Word - ch04三校.doc

山东建筑大学学分制管理规定(试行)

Microsoft PowerPoint - Class4.pptx

Microsoft PowerPoint - 20-string-s.pptx

Microsoft Word - 武術-定稿.doc

C++ 程序设计 实验 2 - 参考答案 MASTER 2017 年 5 月 21 日 1

第一章

Microsoft PowerPoint - C-Ch12.ppt

untitled

ebook39-6

epub 33-8

Microsoft PowerPoint - ch09_AEL0080.ppt

新・明解C言語入門編『索引』

, 即 使 是 在 昏 暗 的 灯 光 下, 她 仍 然 可 以 那 么 耀 眼 我 没 有 地 方 去, 你 会 带 着 我 么 杜 晗 像 是 在 嘲 笑 一 般, 嘴 角 的 一 抹 冷 笑 有 着 不 适 合 这 个 年 龄 的 冷 酷 和 无 情, 看 着 江 华 的 眼 神 毫 无 温

主程式 : public class Main3Activity extends AppCompatActivity { ListView listview; // 先整理資料來源,listitem.xml 需要傳入三種資料 : 圖片 狗狗名字 狗狗生日 // 狗狗圖片 int[] pic =new

Microsoft PowerPoint - 11_Templates.ppt

C/C++ 语言 - 循环

Microsoft PowerPoint - ch04_AEL0080.ppt


Microsoft Word - well_game.doc

Microsoft Word - 04_object_Boxing_property_indexer.doc

1.5招募说明书(草案)

運算子多載 Operator Overloading

<4D F736F F D203136BCADBBD8D2E4D3EBD1D0BEBF2E646F63>

萧山中学课程建设方案.doc


Microsoft Word - 9pinggb_A4.doc

Microsoft Word - 9pinggb_A4-f4.doc

理 论 探 索 事 业 单 位 改 革 的 五 点 思 考 余 路 [ 摘 要 ] 事 业 单 位 改 革 是 中 国 改 革 的 重 要 环 节, 其 影 响 力 和 难 度 不 亚 于 国 有 企 业 改 革 本 文 着 重 围 绕 推 进 事 业 单 位 改 革 应 考 虑 的 五 个 方 面

日 本 位 于 亚 洲 东 部, 太 平 洋 西 北 角, 是 我 国 东 方 的 一 个 岛 国 在 洪 积 世 ( 注 1) 的 大 部 分 时 期 内, 日 本 与 大 陆 相 连 大 约 在 洪 积 世 晚 期 至 冲 积 世 ( 注 2) 初 期, 日 本 各 地 发 生 海 进, 出 现

2深化教育教学改革、创新人才培养模式

Microsoft Word - 9pinggb_let.doc

Microsoft Word - 9pingb5_let.doc

退休權益.ppt [相容模式]

Microsoft Word - 1.《國文》試題評析.doc

Ps22Pdf

$%%& ()*+, %&, %-&&%%,. $ %,, $,, & /$- 0(1 $%%& %& 234 %-%, 5&%6&633 & 3%%, 3-%, %643 -%%% :::; 7<9; %-%, 3$%$ :::;

# $# #!# # # # # # # %# # # &# # # # #! "

2016 年 地 质 工 程 系 教 学 工 作 安 排 2016 学 年 我 系 将 在 总 结 过 去 工 作 的 基 础 上, 结 合 今 年 学 院 以 抓 质 量 强 内 涵 促 改 革 调 结 构 建 品 牌 细 管 理 重 过 程 为 宗 旨, 以 规 范 管 理 深 化 内 涵 为


实 习 上 下 点 表 格 解 释 和 相 关 纪 律 要 求 : 1 表 格 中 所 有 名 词 都 为 简 称, 包 括 医 院 名 称 四 年 级 五 年 级 各 专 业 名 称 等 所 有 时 间 都 为 学 生 装 好 行 李 出 发 时 间, 请 提 前 0 分 钟 将 行 李 运 到

3 基 金 杠 杆 从 分 级 基 金 的 概 念, 我 们 知 道 了 分 级 基 金 的 A 份 额 是 每 年 获 得 固 定 收 益 的 稳 健 份 额,B 份 额 是 具 有 杠 杆 效 应 的 激 进 份 额 分 级 基 金 中 的 杠 杆 一 般 有 三 类 : 份 额 杠 杆 =(A

简报158期.doc

zt

PowerPoint 簡報

<4D F736F F D20AC4FBDBDA4FBB67DA96CAABA2DA743A67EAFC5AAA95FA7B9BD5A5F2E646F63>

ex

Microsoft Word - 01.DOC

untitled

概述

Template

Microsoft PowerPoint - ch03_AEL0080.ppt

untitled

840 提示 Excel - Excel -- Excel (=) Excel ch0.xlsx H5 =D5+E5+F5+G5 (=) = - Excel 00

( CIP) /. :, ( ) ISBN TP CIP ( 2005) : : : : * : : 174 ( A ) : : ( 023) : ( 023)

Microsoft Word - 11.doc


Python a p p l e b e a r c Fruit Animal a p p l e b e a r c 2-2

第七讲 继承与多态

菩提道次第廣論

路 上 沒 說 話, 車 子 被 爸 離 去 後 開 走 了, 沒 什 麼 變, 除 了 一 股 淡 淡 的 香 味, 我 不 太 習 慣, 像 空 氣 中 的 粉 塵, 左 飄 右 飄, 光 中 飛 舞 我 沒 提, 看 車 窗 外, 外 面 不 太 有 趣, 我 只 是 沒 事 幹, 我 們 本

繁 華 國 小 101 學 年 母 親 節 感 恩 惜 福 - 跳 蚤 市 場 暨 科 學 闖 關 遊 戲 親 子 活 動 實 施 計 畫 一 依 據 : 本 校 101 學 年 度 校 務 計 畫 及 行 事 曆 二 目 的 : 1. 培 養 學 生 感 恩 惜 物 知 福 惜 福 的 節 儉 觀

Transcription:

第八 九章 I/O 系統 檔案 I/O 的基本概念 格式化 I/O 建立自訂的嵌入子 建立自訂的擷取子 自訂 I/O 與檔案

檔案 I/O 的基本概念 C++ 對 I/O 的支援是放在 <iostream> 中, 而 Class ios 包括有許多成員函數與變數可以用來控制和監督串流的運作. 要處理檔案 I/O, 心必須 #include<fstream>, 它包含了 ifstream,ofstream and fstream, 而這些 class 是衍生自 istream,ostream 而此兩個 class 是衍生自 ios

檔案 I/O 的基本概念 在 C++ 中, 開啟一個 File 就是鏈結到一個 stream; 而 stream 有三種型態 : ifstream in; // input ofstream out; // output fstream io; // input and output 一旦建立 stream 後, 利用 open() 便可以將 stream 與 file 鏈結在一起.

OPEN() 函數 void ifstream::open(const char *filename,openmode mode=ios::in); void ofstream::open(const char *filename,openmode mode=ios::in ios::trunc); void fstream::open(const char *filename,openmode mode=ios::in ios::out);

Openmode ios::app // 所有的輸出附加到檔案結尾 ios::ate // 將檔案指標移到檔案結尾 ios::binary // 以二進位模式開啟檔案 ios::in // 開啟檔案作為輸入使用 ios::out // 開啟檔案作為輸出使用 ios::trunc // 會覆寫之前同名的檔案

使用 Open 函數開啟檔案 Ofstream mystream; Mystream.open( test ); 判斷開啟與否 If(!mystream){cout<< cannot open file\n ;} 或可利用 is_open() 判斷開啟與否 Bool is_open(); 如果開啟 file 則傳回 true; 否則則傳回 false; If(!mystream.is_open()){{cout<< cannot open file\n ;}

Stream 的建構函數 儘管可以使用 open() 開檔, 但更方便的方法則是使用 ifstream mystream( myfile );//open file 因為在 ifstream,ofstream,fstream 中的建構函數會自動開啟檔案. 判斷是否到檔案結尾則是用 bool eof(); 關閉檔案則是用 close();

檔案內容的讀寫 當 file 己開啟時, 不在使用 cin 與 cout 來讀寫檔案, 而是改成 >> 與 <<, 就像是 c 中的 fprintf() 與 fscan().

開關檔範例 1 #include<iostream> #include<fstream> using namespace std; int main() { ofstream fout("c:\\test.dat"); if(!fout){ cout<<"cannot open file\n";return 1; } fout<<"hello\n"; fout<<100<<' '<<hex<<100<<endl; fout.close(); ifstream fin("c:\\test.dat"); if(!fin){ cout<<"cannot open file\n";return 1; } char str[80]; int i; fin>>str>>i; cout<<str<<' '<<i<<endl; fin.close(); return 0;}

課後練習 寫一程式來計算檔案中的字元個數.

格式化的 I/O 在每個 stream 中都有一格式化 flag 的集合, 可以用來控制資料的格式 ; 在 class ios 中就宣告了一列舉型別 fmtflags 它定義了 skipws // 忽略空白字元 left // 輸出向左靠攏 oct // 八進位輸出表示 hex // 十六進位輸出表示 uppercase // 字元輸出變大寫

格式化的 I/O showpos // 顯示正數前加 + 符號 showpoint // 在 float 時顯示小數點和其後的零 scientific // 以科學符號顯示 fixed // 以一般方式顯示要設定一格式旗標使用 ios 的成員函數 setf() // fmtflags setf(fmtflags flags) ex: stream.setf(ios::showpos);

格式化的 I/O 而 unseft() 函數可用來清除 flag 設定值 void unseft(fmtflags flags); 要了解目前 flag 的設定則使用 flag() 函數 fmtflags flag(); fmtflags flag(fmtflags f);

設定多種旗標格式範例 #include<iostream> using namespace std; int main(){ // show using default setting cout<<123.23<<"hello <<100<<'\n'; cout<<10<<' '<<- 10<<'\n'; cout<<100.0<<"\n\n"; //now, change formats cout.unsetf(ios::dec); cout.setf(ios::hex ios::scie ntific); cout<<123.23 <<"hello" <<100<<'\n'; cout.setf(ios::showpos); cout<<10<<' '<<- 10<<'\n'; cout.setf(ios::showpoint io s::fixed); cout<<100.0<<"\n\n"; return 0; }

改變大小寫範例 #include<iostream> using namespace std; int main(){ cout.unsetf(ios::dec); cout.setf(ios::uppercase ios::showbase ios::hex ); cout<<88<<"\n\n"; cout.unsetf(ios::uppercase); cout<<88<<"\n\n"; return 0; }

練習 寫一程式來設定 cout flag. 在 show float 時可以 show 出小數點 ; 另外以科學技號再加上大寫 E 來 show 出所有的 float.

建立自訂的嵌入子 在 C++ 中, 輸出的動作被叫作嵌入, 而 << 則被稱為嵌入運算子 (insertion operator) ostream& & operator<<(ostream ostream& out, class-name ob) { // body or inserter return stream;}

建立自訂的嵌入子 第一個參數是對 ostream 的參考型態, 這代表 stream 必須要是一個 output stream 第二個參數則代表要輸出的物件 ( 也可以是一參考參數 ) 嵌入函數傳回 stream 的參考, 這是為了讓 << 能在一連串的 I/O 運算中使用 Ex:cout<<ob1<<ob2<<ob3;

建立自訂的嵌入子 由於嵌入子左運算元是一 stream; 而右運算元是要輸出的物件, 如果將嵌入子函數放進 class 變成成員函數, 它的左運算元會自動由 this 指標被設為產生這個呼叫的物件. 因此嵌入子不能放入 class 中. 為了讓嵌入子函數可存取 class 中的成員則嵌入子要變為該 class 中的 friend.

嵌入子範例 - 成為 friend #include<iostream> using namespace std; class coord{ int x,y; public: coord(){x=0;y=0;} coord(int a,int b){x=a;y=b;} friend ostream &operator<<(ostream &stream,const coord& ob); };

嵌入子範例 - 成為 friend ostream &operator<<(ostream &stream, const coord& ob) {stream<<ob.x<<", "<<ob.y<<'\n'; return stream;} int main() { coord a(1,1),b(2,3); cout<< a<< b; return 0;}

嵌入子範例 - 不成為 friend #include<iostream> using namespace std; class coord{ public: int x,y; coord(){x=0;y=0;} coord(int a,int b){x=a;y=b;} // friend ostream &operator<<(ostream &stream, const coord ob); };

嵌入子範例 - 不成為 friend ostream &operator<<(ostream &stream,const coord ob) { stream<<ob.x<<", "<<ob.y<<'\n'; return stream;} int main() { coord a(1,1),b(2,3); cout<<a<<b; return 0; }

建立自訂的擷取子 在 C++ 中, 輸入的動作被叫作擷取運算子 (extraction operator), istream& & operator>>(istream istream& stream, class-name ob) { // body or inserter return stream;}

擷取子範例 #include<iostream.h> //using namespace std; class coord{ int x,y; public: coord(){x=0;y=0;} coord(int a,int b){x=a;y=b;} friend istream &operator>>(istream &stream,coord& ob); friend ostream &operator<<(ostream &stream,coord& ob); };

擷取子範例 istream &operator>>(istream &stream, coord& ob) { cout<<"enter coordinates\n"; stream>>ob.x>>ob.y; return stream; } ostream &operator<<(ostream &stream, coord& ob) { stream<<ob.x<<", "<<ob.y<<'\n'; return stream; }

擷取子範例 int main() { coord a(1,1),b(2,3); cout<<a<<b; cin>>a; cout<<a; return 0; }

自訂 I/O 與檔案 結合開檔與 >> 和 << 兩個運算子 #include<iostream> #include<fstream> using namespace std; class coord{ int x,y; public: coord(){x=0;y=0;} coord(int a,int b){x=a;y=b;} friend ostream &operator<<(ostream &stream, coord &ob); friend istream &operator>>(istream &stream,coord &ob); };

自訂 I/O 與檔案 ostream &operator<<(ostream &stream,coord& ob) { stream<<ob.x <<'\n'; stream<<ob.y<<'\n'; return stream;} istream &operator>>(istream &stream,coord &ob) { stream>>ob.x>>ob.y; return stream;} int main() {

自訂 I/O 與檔案 coord a(1,1),b(2,3); ofstream out("c:\\test.dat"); if(!out){cout<<"cannot open output file\n"; return 1; } out<<a<<b; out.close(); ifstream in("c:\\test.dat"); if(!in){cout<<"cannot open input file\n"; return 1;}

自訂 I/O 與檔案 coord c(0,0),d(0,0); in>>c>>d; cout<<c<<d; in.close(); return 0; }

練習 請撰寫一 class, 它可以儲存一整數值和它最小的因數 (factor), 請加入嵌入子與擷取子.