C++之IO流的状态以及使用

 1 #include <iostream>
 2 #include <fstream>
 3 
 4 using  namespace std;
 5 
 6 void check_cin (istream &is)
 7 {
 8     if(is.bad())//系统级的错误才会bad
 9     {
10         cout<<"cin bad()"<<endl;
11     }else{
12         cout<<"cin not bad()"<<endl;
13     }
14 
15     if(is.fail())//
16     {
17         cout<<"cin fail()"<<endl;
18     }else{
19         cout<<"cin not fail()"<<endl;
20     }
21 
22     if(is.eof())//输入结束的时候是eof
23     {
24         cout<<"cin eof()"<<endl;
25     }else{
26         cout<<"cin not eof()"<<endl;
27     }
28 
29 
30     if(is.good())//
31     {
32         cout<<"cin good()"<<endl;
33     }else{
34         cout<<"cin not good()"<<endl;
35     }
36 
37 }
38 
39 int main()
40 {
41     cout<<"检查cin的状态,一下四个状态"<<endl;
42 //    cin.setstate(istream::badbit); //强制将流的状态设置为bad
43 //    cin.setstate(istream::failbit);
44 //    cin.setstate(istream::badbit | istream::failbit);
45 //    cin.clear(istream::badbit)//可以清除某一个状态
46 
47 
48 //    istream::iostate old_state=cin.rdstate();//将流的状态保存下来
49 //    .........
50 //    cin.clear(old_state)//用完以后然后还原成原来保存下来的状态
51 
52 
53 
54     check_cin(cin);
55     cout<<"请输入一个数"<<endl;
56     int n;
57     cin>>n;
58     cout<<"再次检查状态"<<endl;
59     check_cin(cin);
60 
61     cout<<"============================="<<endl;
62     int i=0,val;
63 //    while(cin>>val)
64 //    {
65 //        i+=val;
66 //        cout<<"sum is"<<i<<endl;
67 //    }
68 
69     while(cin>>val,!cin.eof())
70     {
71         if(cin.bad())
72         {
73             throw runtime_error("IO CORRUPED");
74         }
75         if(cin.fail())
76         {
77             cout<<"输入的数据是错的"<<endl;
78             cin.clear(); //清空恢复成正常的流
79             cin.ignore(200,'\n'); //对不对的数据进行清除要么是200个要么是换行
80             continue; //继续循环
81         }
82         i+=val;
83         cout<<"sum is"<<i<<endl;
84     }
85 
86     return 0;
87     //预处理进行调试
88     #ifdef NDEBUG
89     cout<<""<<endl;
90     #endif // NDEBUG
91 }

 流对象的使用:

 1 #include <iostream>
 2 #include <fstream> //文件流
 3 #include <string>
 4 
 5 using  namespace std;
 6 
 7 
 8 
 9 int main()
10 {
11     //文件输出流
12     ofstream outfile("test.txt");
13     outfile<<"hello file!";
14     outfile.close();
15     //文件输入流
16     string file("one.txt");
17 //    ifstream infile(file.c_str());//此处使用C风格的字符串
18     //上边的可以写成下边的
19     ifstream infile;
20     infile.open(file.c_str());
21 //    if(infile) //如果打开文件成功
22 //    {
23 //
24 //    }
25 
26 //    if(!infile) //打开文件失败
27 //    {
28 //        cerr<<"打开文件失败"<<endl;
29 //        return -1;
30 //    }
31 
32     string s;
33 //    infile >>s; //读出一行
34 //    infile.close();
35     cout<<s<<endl;
36 
37     while(infile>>s) //循环读出所有
38     {
39         cout<<s<<endl;
40     }
41     infile.close();
42     infile.clear();
43     cout<<"======================="<<endl;
44     string s2;
45     string file2("two.txt");
46     infile.open(file2.c_str());
47     if(!infile)
48     {
49         cerr<<"文件打开失败"<<endl;
50         return -1;
51     }
52     while(infile>>s2)
53     {
54         cout<<s2<<endl;
55     }
56     infile.close();
57 
58 
59     return 0;
60 
61 
62 
63 
64 
65 
66 
67     //预处理进行调试
68     #ifdef NDEBUG
69     cout<<""<<endl;
70     #endif // NDEBUG
71 }

猜你喜欢

转载自www.cnblogs.com/yh2924/p/12596437.html