Appearance
C++ 基本的输入输出
C++ 提供了多种输入输出方式,最常用的是std::cin和std::cout,它们定义在<iostream>头文件中。
1. 标准输出流
1.1 std::cout
std::cout是标准输出流,用于向屏幕输出数据。
示例
cpp
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
std::cout << "C++ Programming" << std::endl;
return 0;
}1.2 输出多个值
cpp
#include <iostream>
int main() {
int age = 20;
std::string name = "张三";
std::cout << "姓名: " << name << ", 年龄: " << age << std::endl;
return 0;
}1.3 换行
cpp
#include <iostream>
int main() {
// 使用 std::endl
std::cout << "Hello" << std::endl;
std::cout << "World" << std::endl;
// 使用 \n
std::cout << "Hello\n";
std::cout << "World\n";
return 0;
}1.4 格式化输出
1.4.1 设置精度
cpp
#include <iostream>
#include <iomanip>
int main() {
double pi = 3.141592653589793;
std::cout << "默认精度: " << pi << std::endl;
std::cout << "精度为 5: " << std::setprecision(5) << pi << std::endl;
std::cout << "精度为 10: " << std::setprecision(10) << pi << std::endl;
std::cout << "固定小数点,精度为 5: " << std::fixed << std::setprecision(5) << pi << std::endl;
return 0;
}1.4.2 设置宽度
cpp
#include <iostream>
#include <iomanip>
int main() {
int a = 10;
int b = 20;
int c = 30;
std::cout << "默认宽度:" << std::endl;
std::cout << a << " " << b << " " << c << std::endl;
std::cout << "宽度为 10:" << std::endl;
std::cout << std::setw(10) << a << std::setw(10) << b << std::setw(10) << c << std::endl;
return 0;
}1.4.3 设置填充字符
cpp
#include <iostream>
#include <iomanip>
int main() {
int a = 10;
std::cout << "默认填充:" << std::setw(10) << a << std::endl;
std::cout << "填充为 *:" << std::setw(10) << std::setfill('*') << a << std::endl;
return 0;
}1.4.4 设置对齐方式
cpp
#include <iostream>
#include <iomanip>
int main() {
int a = 10;
std::cout << "默认对齐(右对齐):" << std::setw(10) << a << std::endl;
std::cout << "左对齐:" << std::setw(10) << std::left << a << std::endl;
std::cout << "右对齐:" << std::setw(10) << std::right << a << std::endl;
return 0;
}2. 标准输入流
2.1 std::cin
std::cin是标准输入流,用于从键盘输入数据。
示例
cpp
#include <iostream>
int main() {
int age;
std::cout << "请输入你的年龄: ";
std::cin >> age;
std::cout << "你的年龄是: " << age << std::endl;
return 0;
}2.2 输入多个值
cpp
#include <iostream>
int main() {
int age;
std::string name;
std::cout << "请输入你的姓名和年龄: ";
std::cin >> name >> age;
std::cout << "姓名: " << name << ", 年龄: " << age << std::endl;
return 0;
}2.3 输入字符串
cpp
#include <iostream>
#include <string>
int main() {
std::string name;
std::cout << "请输入你的名字: ";
std::cin >> name; // 遇到空格停止
std::cout << "名字: " << name << std::endl;
return 0;
}2.4 输入整行
cpp
#include <iostream>
#include <string>
int main() {
std::string full_name;
std::cout << "请输入你的全名: ";
std::getline(std::cin, full_name);
std::cout << "全名: " << full_name << std::endl;
return 0;
}2.5 输入字符
cpp
#include <iostream>
int main() {
char ch;
std::cout << "请输入一个字符: ";
std::cin >> ch;
std::cout << "你输入的字符是: " << ch << std::endl;
return 0;
}3. 输入输出错误处理
3.1 检查输入是否成功
cpp
#include <iostream>
int main() {
int age;
std::cout << "请输入你的年龄: ";
std::cin >> age;
if (std::cin.fail()) {
std::cout << "输入错误!" << std::endl;
std::cin.clear();
std::cin.ignore(1000, '\n');
} else {
std::cout << "你的年龄是: " << age << std::endl;
}
return 0;
}3.2 清除错误状态
cpp
#include <iostream>
int main() {
int age;
std::cout << "请输入你的年龄: ";
while (!(std::cin >> age)) {
std::cout << "输入错误,请重新输入: ";
std::cin.clear();
std::cin.ignore(1000, '\n');
}
std::cout << "你的年龄是: " << age << std::endl;
return 0;
}4. 其他输入输出函数
4.1 std::cerr
std::cerr是标准错误流,用于输出错误信息。
cpp
#include <iostream>
int main() {
std::cerr << "这是一个错误信息" << std::endl;
return 0;
}4.2 std::clog
std::clog是标准日志流,用于输出日志信息。
cpp
#include <iostream>
int main() {
std::clog << "这是一个日志信息" << std::endl;
return 0;
}4.3 printf 和 scanf
C++ 也支持 C 风格的输入输出函数。
cpp
#include <cstdio>
int main() {
// printf
printf("Hello, World!\n");
// 格式化输出
int age = 20;
printf("年龄: %d\n", age);
// scanf
int input;
printf("请输入一个数字: ");
scanf("%d", &input);
printf("你输入的数字是: %d\n", input);
return 0;
}5. 文件输入输出
5.1 写入文件
cpp
#include <iostream>
#include <fstream>
int main() {
std::ofstream outfile("output.txt");
if (outfile.is_open()) {
outfile << "Hello, World!" << std::endl;
outfile << "C++ Programming" << std::endl;
outfile.close();
std::cout << "文件写入成功" << std::endl;
} else {
std::cout << "无法打开文件" << std::endl;
}
return 0;
}5.2 读取文件
cpp
#include <iostream>
#include <fstream>
#include <string>
int main() {
std::ifstream infile("output.txt");
if (infile.is_open()) {
std::string line;
while (std::getline(infile, line)) {
std::cout << line << std::endl;
}
infile.close();
} else {
std::cout << "无法打开文件" << std::endl;
}
return 0;
}6. 示例:综合运用
现在,让我们看一个综合运用各种输入输出操作的例子:
cpp
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
int main() {
// 标准输出流
std::cout << "标准输出流:" << std::endl;
std::cout << "Hello, World!" << std::endl;
std::cout << std::endl;
// 输出多个值
std::cout << "输出多个值:" << std::endl;
int age = 20;
std::string name = "张三";
std::cout << "姓名: " << name << ", 年龄: " << age << std::endl;
std::cout << std::endl;
// 格式化输出
std::cout << "格式化输出:" << std::endl;
double pi = 3.141592653589793;
std::cout << "固定小数点,精度为 5: " << std::fixed << std::setprecision(5) << pi << std::endl;
std::cout << std::endl;
// 标准输入流
std::cout << "标准输入流:" << std::endl;
int input_age;
std::cout << "请输入你的年龄: ";
std::cin >> input_age;
std::cout << "你的年龄是: " << input_age << std::endl;
std::cin.ignore(); // 清除缓冲区
std::cout << std::endl;
// 输入整行
std::cout << "输入整行:" << std::endl;
std::string full_name;
std::cout << "请输入你的全名: ";
std::getline(std::cin, full_name);
std::cout << "全名: " << full_name << std::endl;
std::cout << std::endl;
// 输入输出错误处理
std::cout << "输入输出错误处理:" << std::endl;
int number;
std::cout << "请输入一个数字: ";
while (!(std::cin >> number)) {
std::cout << "输入错误,请重新输入: ";
std::cin.clear();
std::cin.ignore(1000, '\n');
}
std::cout << "你输入的数字是: " << number << std::endl;
std::cout << std::endl;
// 标准错误流
std::cerr << "标准错误流:" << std::endl;
std::cerr << "这是一个错误信息" << std::endl;
std::cout << std::endl;
// 标准日志流
std::clog << "标准日志流:" << std::endl;
std::clog << "这是一个日志信息" << std::endl;
std::cout << std::endl;
// 文件输入输出
std::cout << "文件输入输出:" << std::endl;
std::ofstream outfile("output.txt");
if (outfile.is_open()) {
outfile << "Hello, World!" << std::endl;
outfile << "C++ Programming" << std::endl;
outfile.close();
std::cout << "文件写入成功" << std::endl;
}
std::ifstream infile("output.txt");
if (infile.is_open()) {
std::string line;
std::cout << "文件内容:" << std::endl;
while (std::getline(infile, line)) {
std::cout << line << std::endl;
}
infile.close();
}
return 0;
}小结
C++ 基本的输入输出包括:
标准输出流:
std::cout:标准输出流- 输出多个值
- 换行:
std::endl或\n
格式化输出:
- 设置精度:
std::setprecision() - 设置宽度:
std::setw() - 设置填充字符:
std::setfill() - 设置对齐方式:
std::left、std::right
- 设置精度:
标准输入流:
std::cin:标准输入流- 输入多个值
- 输入字符串:
std::cin >> name - 输入整行:
std::getline(std::cin, full_name)
输入输出错误处理:
- 检查输入是否成功:
std::cin.fail() - 清除错误状态:
std::cin.clear() - 忽略输入:
std::cin.ignore()
- 检查输入是否成功:
其他输入输出函数:
std::cerr:标准错误流std::clog:标准日志流printf和scanf:C 风格的输入输出函数
文件输入输出:
- 写入文件:
std::ofstream - 读取文件:
std::ifstream
- 写入文件:
关键概念:
- 标准输出流:
std::cout - 标准输入流:
std::cin - 格式化输出:设置精度、宽度、填充字符、对齐方式
- 输入输出错误处理:检查输入是否成功,清除错误状态
掌握基本的输入输出是编写 C++ 程序的基础,在后续章节中,我们将学习 C++ 的结构体。