首页 > 系统 > iOS > 正文

【C++】iostream, fstream, stringstream知识

2019-11-06 07:47:42
字体:
来源:转载
供稿:网友

以前学习C++的时候, 总是囫囵吞枣地理解cin, cout等东东, 最近又在复习C++,  复习到IO类这一章节的时候, 有点感触, 所以呢, 打算记录一下。

         俗话说, 一图胜过千言万语, 这不是没有道理的, 下面, 我们来看看基本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 plain copy 在CODE上查看代码片#include <iostream>  using namespace std;    int main()  {      int i = -1;            cin >> i; // cin从控制台接收输入, 并保存在i中        cout << i << endl; // count把i的值输出到控制台        return 0;  }  

 %20 %20 很简单很好理解吧。

 %20 %20 2.%20IO类值之fstream

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

[cpp] view%20plain copy #include <iostream>  #include <string>  #include <fstream>  using namespace std;    int main()  {      ifstream in("test.txt"); // 建立in与文件test.txt之间的额关联      if(!in)      {          cout << "error" << endl;          return 1;      }        string line;      while(getline(in, line))      {          cout << line << endl;         }        return 0;  }   %20 %203.%20IO类之stringstream

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

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

lucy%20123 

lili%20234%20456

tom%20222%20456%20535

jim%202345%20675%2034%20654

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

lucy%20123x 

lili%20234x %20 456x 

tom%20222x %20 456x %20 535x 

jim%202345x %20 675x %20 34x %20 654x 

 %20 %20 直接看程序吧:

[cpp] view%20plain copy #include <iostream>  #include <string>  #include <fstream>  #include <sstream>  using namespace std;    int main()  {      ifstream in("test.txt"); // 建立in与文件test.txt之间的额关联      if(!in)      {          cout << "error" << endl;          return 1;      }        string line;       string passWord;      while(getline(in, line))      {          istringstream ss(line); // 建立ss与line之间的关联          int i = 0;          while(ss >> password) // ss从line读取东西并保存在password中          {                 cout << password + (1 == ++i ? "" : "x") << " ";          }                    cout << endl;      }        return 0;  }   %20 %20 %20 结果ok.

 %20 %20 %20 

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

[cpp] view%20plain copy #include <iostream>  #include <string>  #include <sstream>  using namespace std;    int main()  {      int a = -1;      string s = "101";      istringstream is(s); // 建立关联      cout << is.str() << endl; // 101,  看来is和s确实关联起来了啊        is >> a;        cout << a << endl; // 101        return 0;  }  

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

[cpp] view%20plain copy #include <iostream>  #include <string>  #include <sstream>  using namespace std;    int main()  {      string str;      ostringstream os(str); // 建立关联, 但实际没有关联上啊!!!      os << "hello";        cout << str << endl;  // str居然是空, 怎么不是"abc"呢? 我不理解        return 0;  }   %20 %20 看来,%20上面的关联是不成功的,%20改为:

[cpp] view%20plain copy 派生到我的代码片#include <iostream>  #include <string>  #include <sstream>  using namespace std;    int main()  {      int a = -1;      ostringstream os;      os << "hello" << a;        cout << os.str() << endl; // hello-1        return 0;  }        
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表