1
   //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();

帮助我了解当我使用字符串流的 >> 运算符时发生了什么,以及为什么我不能在之后重新使用 << 运算符。

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

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

编辑更多细节:

top10.txt 文件包含:

t04d|120|simon|240|

输出:

Testing contents from file:

Nickname: t04d
Points: 120

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

Nickname: simon
Points: 240

我所期望的;

4

0 回答 0