#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()能有两种不同的参数传递形式,是因为函数的重载特性