C++中的IO类(iostream, fstream, stringstream)小结

来源:互联网 发布:大势至usb端口 编辑:程序博客网 时间:2024/04/27 06:20

俗话说, 一图胜过千言万语, 这不是没有道理的, 下面, 我们来看看基本IO类的继承结构:



      在我们写C++简单代码的时候, 最喜欢写#include <iostream>  , 那么, 这实际上是在包括IO流的头文件, 而用using namespace std;则表示用标准空间, 这样才能用cin,cout, endl等东东啊。

      从上图看, 实际上可以把IO类分为三类:

      1. iostream类: 负责与控制台输入输出打交道, 这个我们已经很熟悉了。   注意: 实际具体又可以区分为:istream和ostream

      2. fstream类:   负责与文件输入输出打交道, 这个我们接触过。  注意: 实际具体又可以区分为:ifstream和ofstream

      3. stringstream类:负责与string上的输入输出打交道, 这个我们暂时还真没用过。  注意: 实际具体又可以区分为:istringstream和ostringstream


       下面, 我们来一一学习/复习:

      1. IO类之iostream

          iostream类的对象, 如cin, cout, 会直接与控制台输入输出关联, 下面我们来看看最简单的程序:

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #include <iostream>  
  2. using namespace std;  
  3.   
  4. int main()  
  5. {  
  6.     int i = -1;  
  7.       
  8.     cin >> i; // cin从控制台接收输入, 并保存在i中  
  9.   
  10.     cout << i << endl; // count把i的值输出到控制台  
  11.   
  12.     return 0;  
  13. }  

     很简单很好理解吧。


     2. IO类值之fstream

      fstream的对象, 与文件建立关联, 我们已经很熟悉了, 直接看代码吧:

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #include <iostream>  
  2. #include <string>  
  3. #include <fstream>  
  4. using namespace std;  
  5.   
  6. int main()  
  7. {  
  8.     ifstream in("test.txt"); // 建立in与文件test.txt之间的额关联  
  9.     if(!in)  
  10.     {  
  11.         cout << "error" << endl;  
  12.         return 1;  
  13.     }  
  14.   
  15.     string line;  
  16.     while(getline(in, line))  
  17.     {  
  18.         cout << line << endl;     
  19.     }  
  20.   
  21.     return 0;  
  22. }  

    3. IO类之stringstream

     stringstream的对象与内存中的string对象建立关联, 往string对象写东西, 或者从string对象读取东西。

     我们先看这样一个问题, 假设test.txt的内容为:

lucy 123 

lili 234 456

tom 222 456 535

jim 2345 675 34 654

     其中每行第一个单词是姓名, 后面的数字都是他们的银行卡密码, 当然啦, jim的银行卡最多, 有4张, 现在, 要实现如下输出, 该怎么做呢?

lucy 123x 

lili 234x   456x 

tom 222x   456x   535x 

jim 2345x   675x   34x   654x 

     直接看程序吧:

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #include <iostream>  
  2. #include <string>  
  3. #include <fstream>  
  4. #include <sstream>  
  5. using namespace std;  
  6.   
  7. int main()  
  8. {  
  9.     ifstream in("test.txt"); // 建立in与文件test.txt之间的额关联  
  10.     if(!in)  
  11.     {  
  12.         cout << "error" << endl;  
  13.         return 1;  
  14.     }  
  15.   
  16.     string line;   
  17.     string password;  
  18.     while(getline(in, line))  
  19.     {  
  20.         istringstream ss(line); // 建立ss与line之间的关联  
  21.         int i = 0;  
  22.         while(ss >> password) // ss从line读取东西并保存在password中  
  23.         {     
  24.             cout << password + (1 == ++i ? "" : "x") << " ";  
  25.         }  
  26.           
  27.         cout << endl;  
  28.     }  
  29.   
  30.     return 0;  
  31. }  

       结果ok.

       

       下面, 我们来看看如何利用istringstream实现字符串向数值的转化:

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #include <iostream>  
  2. #include <string>  
  3. #include <sstream>  
  4. using namespace std;  
  5.   
  6. int main()  
  7. {  
  8.     int a = -1;  
  9.     string s = "101";  
  10.     istringstream is(s); // 建立关联  
  11.     cout << is.str() << endl; // 101,  看来is和s确实关联起来了啊  
  12.   
  13.     is >> a;  
  14.   
  15.     cout << a << endl; // 101  
  16.   
  17.     return 0;  
  18. }  


      当然, 我们也可以把数字格式化为字符串, 如下(下面这个程序有问题):

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #include <iostream>  
  2. #include <string>  
  3. #include <sstream>  
  4. using namespace std;  
  5.   
  6. int main()  
  7. {  
  8.     string str;  
  9.     ostringstream os(str); // 建立关联, 但实际没有关联上啊!!!  
  10.     os << "hello";  
  11.   
  12.     cout << str << endl;  // str居然是空, 怎么不是"abc"呢? 我不理解  
  13.   
  14.     return 0;  
  15. }  
     看来, 上面的关联是不成功的, 改为:

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #include <iostream>  
  2. #include <string>  
  3. #include <sstream>  
  4. using namespace std;  
  5.   
  6. int main()  
  7. {  
  8.     int a = -1;  
  9.     ostringstream os;  
  10.     os << "hello" << a;  
  11.   
  12.     cout << os.str() << endl; // hello-1  
  13.   
  14.     return 0;  
  15. }  

      所以, 我总结呢, istringstream的关联是ok的, 但是ostringstream不能依赖于关联, 不知道C++为啥要这样搞。


      好吧, 到此为止, 总算是对三种类型的IO类有所掌握了。

0 0
原创粉丝点击