在 C++ 中,流(stream)是一種用于實現輸入輸出操作的抽象概念。流可以看作是字節的流動,這些字節可以從一個地方流向另一個地方,例如從鍵盤輸入到程序中,或者從程序輸出到屏幕。C++ 提供了一套完整的流庫來處理各種類型的輸入和輸出,包括文件 I/O、字符串處理等。本文將詳細介紹 C++ 中使用流進行輸入和輸出的方法,并結合實際案例進行講解。
一、輸入輸出流的基本概念
在 C++ 中,流是通過流對象來操作的。流對象是 std
命名空間中的類模板實例化得到的對象,主要包括 istream
、ostream
和 iostream
三種類型。其中,istream
用于輸入操作,ostream
用于輸出操作,而 iostream
是前兩者的組合。
1.1 標準輸入輸出流對象
C++ 預定義了四個標準流對象,分別是 cin
、cout
、cerr
和 clog
。它們分別對應于標準輸入、標準輸出、標準錯誤和標準日志。
示例代碼:
#include <iostream>int main() {// 使用 cout 輸出一段文本std::cout << "Hello, World!" << std::endl;// 使用 cin 讀取一個整數int x;std::cin >> x;std:cout << "You entered: " << x << std::endl;return 0;
}
二、格式化輸入輸出
C++ 提供了一些操縱符(manipulator)來控制輸出格式,例如設置填充字符、對齊方式、寬度等。
2.1 設置輸出格式
示例代碼:
#include <iostream>
#include <iomanip>int main() {int num = 12345;// 設置寬度為 10,向右對齊,不足位用 '0' 補齊std::cout << std::setw(10) << std::right << std::setfill('0') << num << std::endl;// 設置寬度為 10,向左對齊,不足位用空格補齊std::cout << std::setw(10) << std::left << num << std::endl;// 設置輸出為十六進制形式std::cout << std::hex << num << std::endl;return 0;
}
三、文件輸入輸出
C++ 的 fstream
庫提供了用于文件操作的類,包括 ifstream
(用于讀取文件)、ofstream
(用于寫入文件)和 fstream
(同時支持讀寫)。
3.1 文件打開與關閉
示例代碼:
#include <fstream>
#include <iostream>int main() {// 以讀模式打開文件std::ifstream infile("example.txt");if (!infile) {std::cerr << "Error opening file for reading." << std::endl;return 1;}// 以寫模式創建或覆蓋文件std::ofstream outfile("output.txt");if (!outfile) {std::cerr << "Error opening file for writing." << std::endl;return 1;}// 關閉文件infile.close();outfile.close();return 0;
}
案例:讀寫CSV文件
CSV(逗號分隔值)文件是一種常見的數據存儲格式。使用C++流可以方便地讀寫CSV文件。
#include<fstream>
#include<sstream>
#include<vector>
#include<string>std::vector<std::vector<std::string>> readCSV(const std::string& filename) {std::vector<std::vector<std::string>> data;std::ifstream infile(filename);std::string line;while (std::getline(infile, line)) {std::istringstream iss(line);std::vector<std::string> row;std::string value;while (std::getline(iss, value, ',')) {row.push_back(value);}data.push_back(row);}return data;
}void writeCSV(const std::string& filename, const std::vector<std::vector<std::string>>& data) {std::ofstream outfile(filename);for (const auto& row : data) {for (size_t i = 0; i < row.size(); ++i) {outfile<< row[i];if (i < row.size() - 1) {outfile<< ",";}}outfile<< std::endl;}
}int main() {std::vector<std::vector<std::string>> data = readCSV("input.csv");writeCSV("output.csv", data);return 0;
}
四、字符串流
C++ 的 sstream
庫提供了用于字符串操作的類,包括 istringstream
(用于從字符串讀取數據)、ostringstream
(用于向字符串寫入數據)和 stringstream
(同時支持讀寫)。
4.1 字符串讀寫操作
示例代碼:
#include <sstream>
#include <iostream>
#include <string>int main() {// 創建一個字符串流對象std::istringstream iss("Hello, World!");// 從字符串流中讀取數據std::string word;while (iss >> word) {std::cout << word << std::endl;}// 向字符串流中寫入數據std::ostringstream oss;oss << "Number: " << 42;std::string result = oss.str();std::cout << result << std::endl;return 0;
}
五、總結
通過本文的介紹,我們了解了 C++ 中使用流進行輸入和輸出的基本概念和方法。流是 C++ 中處理輸入輸出的重要工具,它提供了一種靈活且高效的方式來處理不同類型的數據。在實際編程中,我們需要根據具體需求選擇合適的流對象和操作方法,以便更好地完成輸入輸出任務。希望本文能夠幫助大家更好地理解和掌握 C++ 中的流操作。