C++之输入与输出
文章目录
- 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++ 中既是流插入运算符也是位左移运算符,注意上下文区分
欢迎给出意见或者相关内容推荐🌹🌹🌹
未完待续…………