1、cout、cin标准输入输出流
cin会创建一个输入缓冲区,键盘向屏幕输入字符的时候,会将数据放进缓冲区。如果缓冲区内没有数据,则会阻塞等待键盘输入。
同样的cout也会有自己的缓冲区,在有的linux编译器下,cout<<"hello world~"<<endl;时,会先将数据放到缓冲区内,收到endl的时候,会将字符输出到屏幕。
除了cout,还有cerr、和clog是没有缓冲区的标准错误,也是输出字符串。
cin.pee();返回第一个字符,但是不从缓冲区取走。可以用于判断,接下来键盘输入的是数字还是字符,或者ascll的其他特殊字符。
cin.ignore(),表示从缓冲区忽略(并取走一个字符)
cin.putback();重新把数据扔回缓冲区的原有位置。
2、cout格式化输出
我们知道在c语言中,有%d, %s等格式换输出。cout也可以完成格式化输出。
![](https://img-blog.csdnimg.cn/e959e264cb4442449eb565306e113020.png)
cout格式化输出,有一种比价繁琐的方式![](https://img-blog.csdnimg.cn/23d3d4a8e9d44118acb6c6462af4bdcd.png)
还有通过控制符的方法
![](https://img-blog.csdnimg.cn/306304b8b1604b7da5d069e8848080d9.png)
![](https://img-blog.csdnimg.cn/eecbd96ec5e64ca4aaf1d1d451d7c9b2.png)
![](https://img-blog.csdnimg.cn/0748633b44b34a6dae2c7d98eb5ebd1c.png)
3、文件读写
![](https://img-blog.csdnimg.cn/d0a2b400ff234f93bc0208b865416137.png)
![](https://img-blog.csdnimg.cn/7e0d4425a3aa444c90c829e203eb1768.png)
#include <iostream>
#include <cstring>
#include <memory>
#include <stdexcept>
#include <fstream> //文件操作
using namespace std;
int main(void)
{
//文本文件读写
char *sourcefile = "./source.txt";
char *outfile = "./out.txt";
ifstream ism(sourcefile, ios::in); //制度方式打开
if(!ism)
{
cout<<"打开"<<sourcefile<<"文件失败"<<endl;
}
ofstream osm(outfile, ios::out);//默认就是文本模式
if(!osm)
{
cout<<"打开"<<outfile<<"文件失败"<<endl;
}
//读文件
char ch;
while (ism.get(ch))
{
osm.put(ch);
}
//关闭文件
ism.close();
osm.close();
//--------------------------------
//二进制文件读写
//ofstream osm(outfile, ios::out | ios::binary);//默认就是文本模式
return 0;
}
![](https://img-blog.csdnimg.cn/5fa849624fc4488db5cc941cca3f6d6d.png)
![](https://img-blog.csdnimg.cn/17d2968e06a947d4b4a30098ec8873c8.png)