一、iostream簡介
iostream是C++標準庫中的重要組成部分,用於在C++程序中進行輸入輸出操作。其中,i代表輸入,o代表輸出,而stream代表了數據流。它是面向對象的,內置了一些底層操作,使得我們可以更方便地進行文件和控制台的輸入輸出操作。
二、iostream的基礎操作
iostream底層使用了緩存機制,所以我們需要使用flush來將緩存中的數據輸出到終端中,同時為了保證終端窗口中數據的雙向通信,需要在每次輸出完畢後使用endl來結束一行語句並刷新緩存。
#include <iostream> using namespace std; int main() { int num = 125; cout << "num的值為:" << num << endl; cout <> in_num; cout << "輸入的整數是:" << in_num << endl; return 0; }
三、iostream的高級操作
iostream還支持對文件操作的輸入輸出。我們可以利用fstream頭文件中的ifstream和ofstream來分別實現文件的讀取和輸出。在打開文件時,需要使用open()函數將文件名和打開方式一起傳入。讀取文件時,使用getline()函數可以逐行讀取文件中的內容。
#include <iostream> #include <fstream> using namespace std; int main() { ofstream outfile; outfile.open("test.txt", ios::out); outfile << "hello world!" << endl; outfile.close(); ifstream infile; infile.open("test.txt", ios::in); string s; if(infile.is_open()){ while(getline(infile, s)){ cout << s << endl; } } infile.close(); return 0; }
四、iostream的格式化操作
iostream支持多種格式化操作,如setw()函數可以設置輸出數據欄位的寬度,setprecision()函數可以設置輸出小數的精度等。
#include <iostream> #include <iomanip> using namespace std; int main() { double pi = 3.1415926535; cout << "不設置精度:" << pi << endl; cout << "設置精度為3:" << setprecision(3) << pi << endl; cout << "欄位寬度為10:" << setw(10) << pi << endl; return 0; }
五、iostream的異常處理
iostream能夠捕獲並處理輸入輸出的異常。例如,如果我們想要輸入一個整數,但用戶輸入了一個非法字元,則會拋出異常並終止程序。我們可以使用try-catch語句來處理異常,並將程序的控制權交還給用戶。
#include <iostream> #include <cstdlib> using namespace std; int main() { try{ int num; cout <> num; cout << "輸入的整數是:" << num << endl; } catch(ios_base::failure &ex){ cout << "輸入的不是整數!" << endl; } return 0; }
六、iostream的運算符重載
我們可以在自己的類中重載iostream的輸入輸出運算符,實現對象在控制台中的輸入輸出。例如,我們可以在一個名為Score的類中重載運算符<>,以實現該類在控制台中的輸入輸出。
#include <iostream> using namespace std; class Score{ public: Score(){} Score(int a, int b){ math = a; english = b; } friend ostream& operator<<(ostream& os, Score& sc) { os << "Math: " << sc.math << " English: " <>(istream& is, Score& sc) { cout<> sc.math >> sc.english; return is; } private: int math; int english; }; int main() { Score x; cin >> x; cout << x << endl; return 0; }
七、iostream的綜合運用
下面是一份iostream的綜合運用代碼示例,包括了文件寫入、讀取、格式化輸出等內容。程序將讀取txt文件中的成績單信息,並將每個學生的平均分輸出到txt文件中。我們同時還可以看到如何使用點操作符、異常處理和重載運算符等高級特性。
#include <iostream> #include <fstream> #include <iomanip> using namespace std; class Student{ public: string name; int math, english; Student(){} Student(string n, int m, int e){ name = n; math = m; english = e; average = (m + e) / 2; } double getAverage(){ return average; } friend ostream& operator<<(ostream& os, Student& sc) { os << setw(10) << sc.name; os << setw(8) << sc.math; os << setw(8) << sc.english; os << setw(10) << setprecision(3) << sc.average <>(istream& is, Student& sc) { is >> sc.name >> sc.math >> sc.english; if (is.fail()) { throw ios_base::failure("無效的輸入!"); } sc.average = (sc.math + sc.english) / 2.0; return is; } private: double average; }; int main() { ifstream infile; infile.open("grades.txt", ios::in); ofstream outfile; outfile.open("result.txt", ios::out | ios::trunc); outfile << "name\t math\t english\t avg" <> s){ try{ outfile << s; } catch (const ios_base::failure &ex){ cerr << ex.what() << endl; } } outfile.close(); infile.close(); return 0; }
原創文章,作者:LOUJK,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/360359.html