C++IO流
一、IO流
流:若干字节数据从一端到另一端叫做流,数据的流动
流类体系
- 流对象
- 流运算符>> <<
二、输入输出流
- ostream类
- cout
- cerr
- clog
- cin
- 字符类的处理
- 正常的操作
- 调用成员函数的方法
- 格式控制符
- 包含头文件iomanip
- 常用的格式控制,一种是调用成员函数方式,一种流控制字符去做
- 设置有效位数:setprecision(n)
- 设置精度:fixed结合setprecision使用
- istream 类 cin
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
using namespace std;
void testOstream() //output
{
//freopen()
cout << "标准输出" << endl; //重定向
cerr << "标准错误输出" << endl; //不能重定向
clog << "标准错误输出" << endl; //重定向为文件
//字符类的处理
cout.put('a');
cout << 'a' << endl;
char c = 'C';
cout.put(c);
cout << c << endl;
cout.write("onetwothree", 5); //指定长度,超过长度不做输出
cout << endl;
//输入
cout.put(cin.get());
cout << endl;
//字符串
while (cin.get() != '\n');
while (getchar() != '\n');
char str[20]="";
cin.getline(str, 20);
cout.write(str,20);
}
void testiomanip()
{
//格式控制符
//设置格式
double pi = 34.12343;
cout << "设置有效位数:" << setprecision(4) << pi <<endl;
cout << "有效小数位"<< fixed << setprecision(4