文章目錄
- C++ 輸入輸出 (I/O) 詳解
- 基本 I/O 組件(input / output)
- 基本輸出 (cout)
- 基本輸入 (cin)
- 格式化輸出
- 文件 I/O
- 字符串流
- 常見 I/O 方法比較
- 錯誤處理
- 其他
- 保留小數
C++ 輸入輸出 (I/O) 詳解
C++ 使用標準庫中的 iostream 庫來處理輸入輸出操作。主要包括以下幾個關鍵組件:
基本 I/O 組件(input / output)
-
標準流對象:
cin
- 標準輸入流 (通常關聯鍵盤)cout
- 標準輸出流 (通常關聯顯示器)cerr
- 標準錯誤流 (無緩沖)clog
- 標準日志流 (有緩沖)
-
頭文件:
#include <iostream> // 基本I/O操作 #include <iomanip> // 格式化輸出
基本輸出 (cout)
#include <iostream>int main() {std::cout << "Hello, World!" << std::endl; // 輸出字符串并換行std::cout << "Number: " << 42 << "\n"; // 輸出多個值,使用\n換行return 0;
}
基本輸入 (cin)
#include <iostream>int main() {int age;std::cout << "Enter your age: ";std::cin >> age; // 從鍵盤讀取輸入std::cout << "You are " << age << " years old.\n";return 0;
}
格式化輸出
使用 <iomanip>
中的操縱符:
#include <iostream>
#include <iomanip>int main() {double pi = 3.141592653589793;// 設置精度(保留4位有效數字)std::cout << std::setprecision(4) << pi << std::endl; // 輸出 3.142// 固定小數位數(保留兩位小數)std::cout << std::fixed << std::setprecision(2) << pi << std::endl; // 輸出 3.14// 設置寬度和對齊std::cout << std::setw(10) << std::left << "Hello" << std::setw(10) << "World" << std::endl;return 0;
}
文件 I/O
使用 <fstream>
頭文件:
#include <iostream>
#include <fstream>int main() {// 寫入文件std::ofstream outFile("example.txt");if (outFile.is_open()) {outFile << "This is a line.\n";outFile << "This is another line.\n";outFile.close();} else {std::cerr << "Unable to open file for writing.\n";}// 讀取文件std::ifstream inFile("example.txt");std::string line;if (inFile.is_open()) {while (getline(inFile, line)) {std::cout << line << '\n';}inFile.close();} else {std::cerr << "Unable to open file for reading.\n";}return 0;
}
字符串流
使用 <sstream>
頭文件:
#include <iostream>
#include <sstream>int main() {// 字符串轉其他類型std::string str = "123.45";std::istringstream iss(str);double num;iss >> num;std::cout << "Number: " << num << std::endl;// 其他類型轉字符串std::ostringstream oss;oss << "The answer is " << 42;std::string result = oss.str();std::cout << result << std::endl;return 0;
}
常見 I/O 方法比較
方法 | 描述 | 適用場景 |
---|---|---|
cin >> var | 格式化輸入 | 讀取基本類型數據 |
getline(cin, str) | 讀取一行 | 讀取字符串,包含空格 |
cin.get() | 讀取單個字符 | 需要逐個字符處理 |
cin.read() | 讀取原始數據 | 二進制數據讀取 |
cout << | 格式化輸出 | 基本輸出 |
cout.write() | 原始數據輸出 | 二進制數據輸出 |
錯誤處理
#include <iostream>int main() {int number;std::cout << "Enter an integer: ";while (!(std::cin >> number)) {std::cin.clear(); // 清除錯誤狀態std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // 忽略錯誤輸入std::cout << "Invalid input. Please enter an integer: ";}std::cout << "You entered: " << number << std::endl;return 0;
}
這些是 C++ 中輸入輸出的基本概念和常用方法。根據具體需求,可以組合使用這些技術來實現復雜的數據輸入輸出操作。
其他
保留小數
使用setprecision()/cout.precision()之后,如果不在修改精度,則之后所有的數據輸出都是按照設置的精度來輸出(fixed同理)
fixed 操作符可能最重要的還是當它與 setprecision 操作符一起使用時,setprecision 即可以以一種新的方式顯示。它將指定浮點數字的小數點后要顯示的位數,而不是要顯示的總有效數位數。而這通常正是我們想要的。
表示浮點輸出應該以固定點或小數點表示法顯示:
#include<bits/stdc++.h>
using namespace std;
int main(){double a;int b;cin>>a>>b;cout<<setprecision(3)<<fixed<<a/b<<endl<<(b<<1);return 0;
}
1,浮點數格式化輸出:
- setprecision(3):設置輸出精度為3位有效數字
- fixed:使用固定小數表示法(小數點表示法)
- 組合使用fixed和setprecision(3)表示保留小數點后3位
2, 位運算:
- b << 1:將b的二進制表示左移1位,相當于乘以2
3, 輸入輸出:
- 輸入兩個數a和b ?
- 第一行輸出a/b的結果(保留3位小數)
- 第二行輸出b左移1位的結果
4,注意事項
- 當b為0時,a/b會導致浮點數除以零錯誤
- fixed會強制以小數形式顯示,即使結果是整數(如4.000)
- << 在C++ 中既是流插入運算符也是位左移運算符,注意上下文區分
歡迎給出意見或者相關內容推薦🌹🌹🌹
未完待續…………