Stream io C default io class C stream

  • Slides: 13
Download presentation
Stream i/o C++ default i/o class

Stream i/o C++ default i/o class

C++ stream i/o classes Template class character based class wide-character based class basic_ios wios

C++ stream i/o classes Template class character based class wide-character based class basic_ios wios basic_istream wistream basic_ostream wostream basic_iostream wiostream basic_fstream wfstream basic_ifstream wifstream basic_ofstream wofstream

Predefined stream objects cin standard input keyboard cout standard output monitor cerr standard error

Predefined stream objects cin standard input keyboard cout standard output monitor cerr standard error output monitor clog Buffered version of cerr monitor When a C++ program being execution, four built-in streams are automatically opend, corresponding to stdin, stdout, stderr in C.

Using stream i/o #include <iostream> using namespace std; int main() { int a =

Using stream i/o #include <iostream> using namespace std; int main() { int a = 2; float b = 3. 4; double c = 2. 717281828; char d = ‘t’, str[10]=“This is a string”; cout << a << b << c << d << str << endl; cout << "type c : " ; cin >> c; cout << "type a string : " ; cin >> str; cout << c << " " << str << endl; system("pause"); example } return 0;

cin -- an object of class basic_istream<char> cout -- an object of class basic_ostream<char>

cin -- an object of class basic_istream<char> cout -- an object of class basic_ostream<char> >> , << 為 class 的 operator>>, operator<<. cout << a; 也可以寫成 cout. operator<<(a); cin >> b; 也可以寫成 cin. operator>>(b); 在以 function overload 的方式處理不同 type 的 a 和 b. To format the stream i/o 1. Using format flags (members of basic_ios<char> class) 2. Member functions in iostream classes. 3. Using member functions in iomanip classes.

Set i/o flag in basic_ios adjustfield hex showbasefield internal showpoint boolalpha left showpos dec

Set i/o flag in basic_ios adjustfield hex showbasefield internal showpoint boolalpha left showpos dec oct skipws fixed right unitbuf floatfield scientific uppercase Syntax: Set flags: cout. setf( ios: : flag 1 | ios: : flag 2 | ios: : flag 1, ios: : flag 2); Clear flags: cout. unsetf( ios: : flag 1 | ios: : flag 2 | ios: : flag 1, ios: : flag 2);

example int a = 100; double c = 2. 717; cout. setf(ios: : hex,

example int a = 100; double c = 2. 717; cout. setf(ios: : hex, ios: : basefield); cout << a << endl; // 64 cout. setf(ios: : showbase); cout << a << endl; // 0 x 64 cout. setf(ios: : uppercase | ios: : scientific); cout << c << endl; // 2. 717 E+000 cout. setf(ios: : showpos); cout << c << endl; // +2. 717 E+000 cout. unsetf(ios: : showpos | ios: : uppercase); cout << c << endl; // 2. 717 e+000

格式控制 (三) Manupilator #include <iomanip> left endl dec fixed showpos skipws internal right ends

格式控制 (三) Manupilator #include <iomanip> left endl dec fixed showpos skipws internal right ends hex scientific noshowpos noskipws boolalpha cout cout << << << setw(n) flush oct showpoint setprecision(n) setfill(char) uppercase nouppercase showbase noshowpoint setiosflags(ios: : floag) (cin) noboolalpha unitbuf setprecision(10) << a << endl; setw(30) << a << endl; setfill('-'); left << setw(30) << a << endl; setw(30) << b << " "; setw(30) << c << endl; nounitbuf example

Overload cout << (binary operator) ostream &operator<< (ostream &stream, class_type object) { stream <<

Overload cout << (binary operator) ostream &operator<< (ostream &stream, class_type object) { stream << object. member; . . return stream; } 1. 2. 3. 4. 5. Operator<< 必須宣告為 passed by reference Ostream 為一 basic_ios <char> 的 class. 第一個幅數 cout 也必須宣告為 pass by reference (連接到 stdout). 第二個幅數是要被列印的 class 物件. 若列印的內容有需要用到 class 的 protected member 必須將 << 宣告為 class 的 friend: friend ostream &operator<< (ostream &, class_type);

Overload cin >> (binary operator) istream &operator>> (istream &stream, class_type &object) { cout <<

Overload cin >> (binary operator) istream &operator>> (istream &stream, class_type &object) { cout << “type member 1 : “; stream >> object. member 1; . . return stream; } 1. 2. 3. 4. 5. Operator>> 必須宣告為 passed by reference istream 為一 basic_ios <char> 的 class. 第一個幅數 cin 也必須宣告為 pass by reference (連接到 stdin). 第二個幅數是要被存放的 class 物件, 也必須 pass by reference. 若存放過程有需要用到 class 的 protected member 必須將 >> 宣告為 class 的 friend: friend istream &operator>> (istream &, class_type &);

練習. . 主程式範例 1 主程式範例 2 請寫 position class 的 cin >> 和 cout

練習. . 主程式範例 1 主程式範例 2 請寫 position class 的 cin >> 和 cout << 的 overload operators. position r 1, r 2; cin >> r 1; cin >> r 2; cout << (r 1*r 2) << endl; cout << (r 1+r 2) << endl; 並思考如何控制 cout 的格式: 因為 ostream operator<< 是寫 在 class 定義檔中 (假想你是 developer, 並不希望 programmer 去更動 class 的 code. ) 所以必須在 class 內加添一些 member 變數用以控制 cout 的格式 (e. g. width), 並加入適當的 member functions 讓 programmer 可以從外面改變格式參數. 請注意在 class 的 constructors 中也要 initial 此參數之值.

Text File stream Input text from a file: Output text to a file: #include

Text File stream Input text from a file: Output text to a file: #include <fstream> main() {. . . ; ifstream inf(“file_name”); main() {. . . ; ofstream outf(“file_name”); inf >> a >> b >> c; outf << a << b << c; for (i=0; i<10; i++) outf << aary[i]; for (i=0; i<10; i++) inf >> aary[i]; } inf. close(); . . . ; } outf. close(); . . . ;