使用>> operator停止我的stringstream以从文件流中获取字符

时间:2015-11-08 23:44:02

标签: c++ stringstream ostringstream

   //I know it is bad using (namespace std) but I'm editing a "attached project"
   // as exercise from my Professor.

   //All required libraries: iostream, fstream, sstream, stdlib are included.

   stringstream stringas;
   ifstream fromfile;
   string nickname;
   float points;

      nickname="";
      points=0;
      fromfile.open ("top10.txt", ifstream::in);

      cout << "Testing contents from file : " << endl;

      while (fromfile.good()){

          if((char) fromfile.get() != '|' )
           {
                fromfile.unget();
                stringas << (char) fromfile.get();
           }
           else
           {
               if(nickname=="")
               {
                nickname = stringas.str(); 
                //or i want use stringas >> nickname;
                stringas.str(string()); 
               }
               else
               {

                stringas >> points; 
                //Problem starts here (when I use >> operator)
                //then I extract all chars from "ss object"
                //Why i can't get Others chars from file when 
                //while(fromfile.good()) starts again?

                stringas.str(string());

               cout << "Nickname: " << nome << endl;
               cout << "Points: " << points << endl;

               topplayers.insert(nickname,points); 
               //topplayers is a Custom(mine) Linked List of 
               // struct type {string nicks,float scores} defined in private
               // in a class(where there is this function).

               }

           }

      }

fromfile.close();

帮助我了解使用&gt;&gt;时发生的情况字符串流的运算符 为什么我不能重复使用&lt;&lt;运营商之后。

我是一名意大利学生,抱歉我的英语不好。

我知道我可以通过其他方式使用文件中的分隔符进行解析,但我需要一种简单的方法,而不需要使用太多的变量和库,例如Boost。

编辑更多细节:

top10.txt文件包含:

t04d|120|simon|240|

输出中:

Testing contents from file:

Nickname: t04d
Points: 120

应用程序运行没有错误,但显然不在屏幕上打印

Nickname: simon
Points: 240

我的预期;

0 个答案:

没有答案