IO C File stream stream ifstream in input
- Slides: 32
檔案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 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; 否則則傳回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){ 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);
設定多種旗標格式範例 #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: : 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 or inserter return stream; }
嵌入子範例-成為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& 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 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; }
自訂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 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; }
- C ifstream
- File-file yang dibuat oleh user pada jenis file di linux
- Woafont=dosapp.fon
- Include fstream
- Ifstream
- Differentiate byte stream and character stream
- The randomaccessfile class treats a file as a stream of
- Google drive sync shared folders to desktop
- The randomaccessfile class treats a file as a stream of
- C# views each file as a sequential stream of bytes.
- Streams in c++
- Meletakkan posisi yang telah ditandai dengan mark()
- Input stream in java
- An html file is a text file containing small markup tags
- In a file-oriented information system, a transaction file
- Physical image vs logical image
- Distributed file system definition
- File sharing management system
- V-file-input rules
- Java file input output
- Java file input and output
- Abaqus input file format
- V-file-input
- V file input
- Jenis peripheral input
- Finely tuned input
- Cipher
- Dfg stream age
- Top k elements in a stream
- Jboss eap dockerfile
- Stream meanders
- Acme stamping value stream mapping
- Stream of conscoiusness