1.流的概念和流类库的结构(了解)
标准IO:对系统的标准输入输出设备进行读写
文件IO:对磁盘进行输入输出读写
串IO:对内存进行读写
2.成员函数
cin.get() //一次只能读取一个字符
cin.get(一个参数) //读一个字符
cin.get(两个参数) //可以读字符串
cin.getline()//取一行,换行符丢弃
cin.ignore()//忽略
cin.peek()//偷窥
cin.putback()//放回
cin.clear()
cin.sync()
cin.fail()
#include <iostream>
#include <iomanip>
using namespace std;
//判断用户输入的是字符串还是数字
void test06()
{
cout << "请输入一个字符串或数字" << endl;
char c=cin.peek();
if (c >= '0'&&c <= '9')
{
int num;
cin >> num;
cout << "输入的数字是:" << num << endl;
}
else
{
char buf[1024] = { 0 };
cin >> buf;
cout << "输入的字符串是:" <<buf << endl;
}
}
//输入一个0到10的数字,直到输入正确为止
void test07()
{
int num;
while (1)
{
cin >> num;
if (num >= 0 && num <= 10)
{
cout << "输入正确" << endl;
break;
}
cout << "重新输入:" << endl;
//重置标志位
cin.clear();
//清空缓冲区
//cin.sync();
//2015
char buf[1024] = { 0 };
cin.getline(buf, 1024);
//打印标志位
cout << cin.fail() << endl;
}
}
系统标准的输出流
1.通过成员函数格式化输出
//通过流成员函数实现格式化的输出
void test03()
{
int num = 99;
cout.width(20);//设置宽度
cout.fill('*');//填充
cout.setf(ios::left);//让数据在左边
cout.unsetf(ios::dec);//卸载十进制
cout.setf(ios::hex);//安装十六进制
cout.setf(ios::showbase);//显示基数
cout.unsetf(ios::hex);//卸载十六进制
cout.setf(ios::oct);//安装八进制
cout << num << endl;
}
2.通过控制符格式化输出
//通过控制符来格式化输出,引入头文件iomanip
void test04()
{
int num = 99;
cout << setw(20);//设置宽度
cout << setfill('~');//填充
cout << setiosflags(ios::showbase);//显示基数
cout << setiosflags(ios::left);//让数据在左边
cout << hex;//六十进制
cout << oct;//八进制
cout << dec;//十进制
cout << num << endl;
}
3.打印浮点数后面的小数点
void test05()
{
double d = 20.22;
cout << setiosflags(ios::fixed);//设置显示浮点数
cout << setprecision(10);//显示小数点后10位
cout << d << endl;
}