ca73a_c++_流的条件状态

/*ca73a_c++_流的条件状态
strm::iostate
strm::badbit //流的状态
strm::failbit //输入的状态,应该输入数字,结果输入为字符,
strm::eofbit

s.eof()
s.fail()
s.bad()
s.good()
s.clear()////恢复流的状态
s.clear(flag)
cin.ignore(200,'\n');//清除前200个字符,如果遇到了‘\n’就提前结束
s.setstate(flag)
s.rdstate()
cin.setstate(ifstream::badbit);//应该写成(istream::badbit)
//error C2027: 使用了未定义类型“std::basic_ifstream<char,std::char_traits<char>>”

//强制把流的状态变成bad方法:用的二进制位来标志它的状态:如:000111000
//cin.setstate(istream::badbit);
//error C2027: 使用了未定义类型“std::basic_ifstream<char,std::char_traits<char>>”
//cin.setstate(istream::failbit);//输入的状态,应该输入数字,结果输入为字符,

//同时改变两个
//cin.setstate(istream::badbit|istream::failbit);

//清除bad
//cin.clear(istream::badbit);
//cin.clear(istream::failbit);

//保存流的状态
istream::iostate old_state = cin.rdstate();
//然后使用old_state,
//使用完成后。再恢复
//cin.clear(old_state);

*/

  1 /*ca73a_c++_流的条件状态
  2 strm::iostate 
  3 strm::badbit  //流的状态
  4 strm::failbit  //输入的状态,应该输入数字,结果输入为字符,
  5 strm::eofbit
  6 
  7 s.eof()
  8 s.fail()
  9 s.bad()
 10 s.good()
 11 s.clear()////恢复流的状态
 12 s.clear(flag)
 13 cin.ignore(200,'\n');//清除前200个字符,如果遇到了‘\n’就提前结束
 14 s.setstate(flag)
 15 s.rdstate()
 16 cin.setstate(ifstream::badbit);//应该写成(istream::badbit)
 17     //error C2027: 使用了未定义类型“std::basic_ifstream<char,std::char_traits<char>>”
 18 
 19         //强制把流的状态变成bad方法:用的二进制位来标志它的状态:如:000111000
 20     //cin.setstate(istream::badbit);
 21     //error C2027: 使用了未定义类型“std::basic_ifstream<char,std::char_traits<char>>”
 22     //cin.setstate(istream::failbit);//输入的状态,应该输入数字,结果输入为字符,
 23 
 24     //同时改变两个
 25     //cin.setstate(istream::badbit|istream::failbit);
 26 
 27     //清除bad
 28     //cin.clear(istream::badbit);
 29     //cin.clear(istream::failbit);
 30 
 31     //保存流的状态
 32     istream::iostate old_state = cin.rdstate();
 33     //然后使用old_state,
 34     //使用完成后。再恢复
 35     //cin.clear(old_state);
 36 
 37 */
 38 #include <iostream>
 39 
 40 using namespace std;
 41 
 42 void check_cin_state(istream& is)
 43 {
 44     
 45     if (is.bad())//检查cin是否有严重的问题,坏了。系统级错误时
 46     {
 47         cout << "cin bad()" << endl;
 48     }
 49     else
 50         cout << "cin is not bad()" << endl;
 51     if (is.fail())
 52         cout << "输入数字,但输入的其它的,如果不能转成数字,就fail" << endl;
 53     else
 54         cout << "cin not fail()" << endl;
 55     if (is.eof())//判断 ctrl+z,结束符
 56         cout << "cin eof()" << endl;
 57     else
 58         cout << "cin not eof()" << endl;
 59     if (is.good())
 60         cout << "cin good()" << endl;//没出问题,就是good
 61     else
 62         cout << "cin not good()" << endl;
 63 }
 64 
 65 int main()
 66 {
 67     cout << "检查cin的状态: " << endl;
 68     //强制把流的状态变成bad方法:用的二进制位来标志它的状态:如:000111000
 69     //cin.setstate(istream::badbit);
 70     //error C2027: 使用了未定义类型“std::basic_ifstream<char,std::char_traits<char>>”
 71     //cin.setstate(istream::failbit);//输入的状态,应该输入数字,结果输入为字符,
 72     
 73     //同时改变两个
 74     //cin.setstate(istream::badbit|istream::failbit);
 75 
 76     //清除bad
 77     //cin.clear(istream::badbit);
 78     //cin.clear(istream::failbit);
 79 
 80     //保存流的状态
 81     istream::iostate old_state = cin.rdstate();
 82     //然后使用old_state,
 83     //使用完成后。再恢复
 84     //cin.clear(old_state);
 85     check_cin_state(cin);
 86     cout << "请输入一个整数:" << endl;
 87     int n;
 88     cin >> n;
 89     cout << "再检查一次cin的状态: " << endl;
 90     check_cin_state(cin);
 91     //demo2
 92     int sum = 0, value;
 93     //while (cin >> value) //如果输入字母,条件不成立,就退出循环了。
 94     //{
 95     //    sum += value;
 96     //    cout << "Sum is : " << sum << endl;
 97     //}
 98     while (cin >> value, !cin.eof())////如果输入字母,即使条件不成立,不让退出循环的方法。
 99     {
100         //!cin.eof(),逗号表达式,最后一个条件决定
101         if (cin.bad())
102             throw std::runtime_error("IO stream corrupted");
103         if (cin.fail())
104         {
105             cerr << "bad data,try again" << endl;
106             //输入错误后,cin就不能用了,及时标志flag是bad,
107             cin.clear();//恢复流的状态
108             cin.ignore(200,'\n');//清除前200个字符,如果遇到了‘\n’就提前结束
109             continue;
110 
111         }
112         sum += value;
113         cout <<"Sum is : " << sum << endl;
114     }
115 
116     return 0;
117 }

 

posted @ 2020-02-10 18:37  txwtech  阅读(221)  评论(0编辑  收藏  举报