IO C File stream stream ifstream in input

  • Slides: 32
Download presentation

檔案I/O的基本概念 在C++中, 開啟一個File就是鏈結到一個 stream; 而stream有三種型態: ifstream in; // input ofstream out; // output fstream

檔案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=ios: : in); void ofstream: : open(const

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

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

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

開關檔範例1 #include<iostream> #include<fstream> using namespace std; int main() { ofstream fout("c: \test. dat"); if(!fout){

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

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

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

設定多種旗標格式範例 #include<iostream> using namespace std; int main(){ // show using default setting cout<<123. 23<<"hello“

設定多種旗標格式範例 #include<iostream> using namespace std; int main(){ // show using default setting cout<<123. 23<<"hello“ <<100<<'n'; cout<<10<<' '<<10<<'n'; cout<<100. 0<<"nn"; //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<<"nn"; return 0; }

改變大小寫範例 #include<iostream> using namespace std; int main(){ cout. unsetf(ios: : dec); cout. setf(ios: :

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

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

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

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

嵌入子範例-成為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;

嵌入子範例-成為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; }

嵌入子範例-不成為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

嵌入子範例-不成為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& stream, class-name ob) { // body or

建立自訂的擷取子 在C++中, 輸入的動作被叫作擷取運算子 (extraction operator), istream& operator>>(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;

擷取子範例 #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 coordinatesn"; stream>>ob. x>>ob. y; return stream;

擷取子範例 istream &operator>>(istream &stream, coord& ob) { cout<<"Enter coordinatesn"; 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;

擷取子範例 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;

自訂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;

自訂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 filen";

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

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

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