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也可以完成格式化输出。
cout格式化输出,有一种比价繁琐的方式
还有通过控制符的方法
3、文件读写
#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;
}