130 likes | 280 Views
Stream i/o. C++ default i/o class. C++ stream i/o classes. Predefined stream objects. cin standard input keyboard cout standard output monitor cerr standard error output monitor clog Buffered version of cerr monitor.
E N D
Stream i/o C++ default i/o class
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 = 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"); return 0; } example
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 • Using format flags (members of basic_ios<char> class) • Member functions in iostream classes. • Using member functions in iomanip classes.
Set i/o flag in basic_ios adjustfield basefield boolalpha dec fixed floatfield hex internal left oct right scientific showbase showpoint showpos skipws unitbuf uppercase Syntax: Set flags: cout.setf( ios::flag1 | ios::flag2 | ios::flag1, ios::flag2); Clear flags: cout.unsetf( ios::flag1 | ios::flag2 | ios::flag1, ios::flag2);
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 << endl; // 0x64 cout.setf(ios::uppercase | ios::scientific); cout << c << endl; // 2.717E+000 cout.setf(ios::showpos); cout << c << endl; // +2.717E+000 cout.unsetf(ios::showpos | ios::uppercase); cout << c << endl; // 2.717e+000
輸出格式控制 (二) cout.width(n) : 預設寬度為字串長度, cout.width 改變輸出的長度 但其效用僅達於下一個 cout. 因此若要控制寬度, 每一個 cout 前都要加 width 指令. cout.precision(n) : 改變有效為數為 n (整數位數 + 小數位數). cout.fill(char) : width 內空白位置, 用字元 char 填滿 example
格式控制 (三) Manupilator #include <iomanip> left right setw(n) setprecision(n) setfill(char) endl ends flush uppercase nouppercase dec hex oct showbase noshowbase fixed scientific showpoint noshowpoint showpos noshowpos setiosflags(ios::floag) skipws noskipws (cin) internal boolalpha noboolalpha unitbuf nounitbuf cout << setprecision(10) << a << endl; cout << setw(30) << a << endl; cout << setfill('-'); cout << left << setw(30) << a << endl; cout << setw(30) << b << " "; cout << setw(30) << c << endl; example
Overload cout << (binary operator) ostream &operator<< (ostream &stream, class_type object) { stream << object.member; . . . . . . . . . . return stream; } • 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 << “type member1 : “; stream >> object.member1; . . . . . . . . . . return stream; } • 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 << 的 overload operators. position r1, r2; cin >> r1; cin >> r2; cout << (r1*r2) << endl; cout << (r1+r2) << endl; 並思考如何控制 cout 的格式: 因為 ostream operator<< 是寫 在 class 定義檔中 (假想你是 developer, 並不希望 programmer 去更動 class 的 code.) 所以必須在 class 內加添一些 member 變數用以控制 cout 的格式 (e.g. width), 並加入適當的 member functions 讓 programmer 可以從外面改變格式參數. 請注意在 class 的 constructors 中也要 initial 此參數之值.
Text File stream Output text to a file: #include <fstream> main() { . . .; ofstream outf(“file_name”); outf << a << b << c; for (i=0; i<10; i++) outf << aary[i]; outf.close(); . . .; } Input text from a file: #include <fstream> main() { . . .; ifstream inf(“file_name”); inf >> a >> b >> c; for (i=0; i<10; i++) inf >> aary[i]; inf.close(); . . .; }