检测文件尾(EOF)

#include<iostream>
using namespace std;
int main()
{
	cout << "please enter whatever you want:" << endl;
	cout << "and if you wanna stop and check out how many characters you've entered ,please press <CTRL><z> at the same time and then press <ENTER>" << endl;
	char ch;
	int counter=0;
	cin.get(ch);
	while (cin.fail() == false)
	{
		cout << ch;
		cin.get(ch);
		counter++;
	}
	cout << endl << counter << "characters read" << endl;
	return 0;
}
  • 利用键盘模拟文件尾操作
    检测到EOF后,cin.eof()将返回bool值true,否则返回false。
    cin.fail()和cin.eof()相似,都用于检测EOF,但是cin.fail()可应用于更多实现中。

**注意:cin.fail()和cin.eof()都采用事后报告,而不是预先报告,因此应该把它们放在读取后 **

  • cin.get()的两种版本
    第一种:cin.get(数组名,数组长度);cin.get();
    第二种:cin.get(char类型参数);
    cin.get()能有两种不同的参数传递形式,是因为函数的重载特性
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值