IOstream基本概念

1.stream对象

c++ I/O由stream完成,所谓stream就是一条数据流,字符序列在其中‘川流不息’。按照面向对象原则,stream是由摸个类定义出来的具有特定性质的对象。输出操作被解读为“数据流入stream” , 输入操作则是“数据流出stream”。另外有一些为标准I/O通道而定义的全局对象。

2.stream类别

class istream

用来定义input stream,可用来数据读取

class ostream

定义output stream, 可用来写出数据


两者分别具体实现自templete class baisc_istream<> ,templete class baisc_ostream<> , 以char作为字符型别。


3.全局性的stream对象

cin

cout

cerr

cerr是所有错误信息所使用的标准错误输出通道,对应于cstdcerr,操作系统也将他们于监视器连接。缺省情况下cerr无缓冲装置。

clog

clog是标准日志通道,c没有对应物。缺省情况下操作系统将它连接于cerr所连接的装置。


4.stream操作符

例如: 

int a,b;

while(std:: cin >> a >>b){

std::cout<<"a:"<<a <<"b:"<<b<<endl;

}


5.操作器

endl

类别   ostream  意义  :输出‘\n’,并刷新output缓冲区

ends

类别   ostream  意义  ‘输出\0’

flush

类别   ostream  意义  刷新output缓冲区

ws

类别   istream  意义  读入并忽略空格


6.stream相关类别及阶层体系



 7.一个简单的使用stream class 的例子:

#include<cstdlib>
#include<iostream>
using namespace std;


int main()
{
double x, y;
cout << "Multiplication of two floating ppoint values" << endl;


cout << "first operand:";
if (!(cin >> x)){
cerr << "error while reading the first floating values" << endl;
return  EXIT_FAILURE;
}




cout << "second operand:";
if (!(cin >> y)){
cerr << "error message reaing the second floating values" << endl;
return  EXIT_FAILURE;
}


cout << x << "times " << y << "equal " << x * y << endl;


}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值