//以下是一个例子,假设从标准输入的是:aab asdf asdf asdf asdf
#include <iostream>
#include <sstream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
int i_number;
cout << "input i_number:"<<endl;
while(cin >> i_number)
{cout << i_number << endl;} //这里cin会把所有的字符输入到i_number里面(因为输入字符,所以i_number得到0)。包括空格(这里把输入的第一个空格以前的所有字符给它,然后其余的所有字符都存在cin中危险哦,所以导致cin流发生错误)
cout<<"inumber_is:" <<i_number << endl;
cout << "cin 's rdstate:" << cin.rdstate() << endl; //流发色错误,那么failbit被置位,所以这里值是4
ios::iostate old_state = cin.rdstate(); //记住cin此时的错误,把他放在ios::iostate(这是个类型,可以存放所有的标志位类型)类型的old_state内
ios::iostate a = cin.goodbit; //定义a代表常量 goodbit的值 0
ios::iostate b = cin.badbit; //定义b代表常量 badbit的值 1
ios::iostate c = cin.eofbit; //定义c代表常量 eofbit的值 2
istream::iostate d = cin.failbit; //定义d代表常量 failbit的值 4
cout << a << endl;
cout << b << endl;
cout << c << endl;
cout << d << endl;
cout << "after been given character,cin 's state is: " << cin.rdstate() << endl;
ifstream fff("2.txt"); //定义一个ifstream(读取用)对象,准备读取文件2.txt内容
ofstream f2("2.txt"); //定义一个ofstream(写入用) 对象,准备写入到文件2.txt
string str3; //定一个一个string对象
fff >> str3; //从文件流对象fff读入到str3
cout << fff.rdstate() << endl; //at last ,his ' s state is 6;
//f2.clear(cin.rdstate());
cout << "cin'rdstate " <<cin.rdstate() << endl; //输出此时的cin流的状态信息
cout << "fff.rdstate*() is:"<<fff.rdstate() << endl; //输出fff流的状态信息
fff.setstate(cin.rdstate()); //把流cin的状态信息添加到fff中(fff原有状态信息不变)
fff.clear(cin.rdstate()); //用cin状态信息替换fff状态信息
cout << fff.rdstate() << endl;
char ch;
cout <<"cin.rdstate() is:" <<cin.rdstate() << endl;
cin.clear(); //复位cin
string s5;
cin >> s5;
cout << "s5:"<< endl;
cout <<"after clear:"<< cin.rdstate() << endl;
cout << "before while loop: there" << endl;
while((ch = cin.get()) != '\n') //输出内存中cin中的所有字符(cin有一部分内容残留)
cout << "has char: " << ch << endl;
cin >> s5;
cout << "s5 comes again:" << s5<< endl;
return 0;
}