title: Read from file after end-of-file is reached
body: <p>I have a function named <code>readNextString(ifstream &amp;file , char* &amp;pBuffer)</code> which extracts the next string from a file, until <code>','</code> or <code>'\n'</code> is reached, removes whitespace at the beginning and at the end of the string, saves the rest in pBuffer and returns true if everything worked fine - false otherwise. Everything works until the end-of-file is reached. When the <code>eof</code> flag is set I can't move my get pointer.
  I've tried this:</p>
  
  <pre><code>if(file.eof())
  {
     file.clear();
     file.seekg(0 , ios::end)
  }
  </code></pre>
  
  <p>...and then remove whitespaces at the end of the string. This almost helped. The function extracts the string without the spaces but I get an infinite loop.</p>
  
  <p>My actual question is: How can I check if the next char is <code>EOF</code>, and if I can't - is there any alternative way to do this?</p>
  
  <p>Here is my actual function:</p>
  
  <pre><code>bool readNextString(ifstream &amp;file , char* &amp;pBuffer)
  {
      if(file.eof()){
          return false;
      }
      for(; file.good() &amp;&amp; isWhitespace(file.peek()) &amp;&amp; !file.eof() ; file.seekg(1 , ios::cur))
          ;
      if(file.eof()){
          cout &lt;&lt; "The file is empty.\n";
          return false;
      }else{
          streamoff startPos = file.tellg();
          cout &lt;&lt; "startPos : " &lt;&lt; startPos &lt;&lt; endl;
          for(;file.good() &amp;&amp; file.peek()!='\n' &amp;&amp; file.peek()!=',' &amp;&amp; file.peek()!= EOF; file.seekg(1 , ios::cur))
              ;
          streamoff A = file.tellg();
          cout &lt;&lt; "A : " &lt;&lt; A &lt;&lt; endl;
          file.seekg(-1 , ios::cur);
          for(;file.good() &amp;&amp; isWhitespace(file.peek()) ; file.seekg(-1 , ios::cur))
              ;
          file.seekg(2 , ios::cur);
          streamoff endPos = file.tellg();
          cout &lt;&lt; "endPos : " &lt;&lt; endPos &lt;&lt; endl;
          pBuffer = new char[endPos-startPos];
          if(pBuffer)
          {
              file.seekg(startPos , ios::beg);
              file.get(pBuffer , endPos-startPos , ',' || '\n');
              for(;file.good() &amp;&amp; file.peek()!='\n' &amp;&amp; file.peek()!=',' &amp;&amp; file.peek()!= EOF; file.seekg(1 , ios::cur))
                  ;
              file.seekg(2 , ios::cur);
              streamoff temp = file.tellg();
              cout &lt;&lt; "temp : " &lt;&lt; temp &lt;&lt; endl;
              return true;
          }else{
              cout &lt;&lt; "Error! Not enough memory to complete the task.\nPlease close some applications and try again.\n";
              return false;
          }
      }
  }
  </code></pre>
  
  <p>And that's one place where I call it:</p>
  
  <pre><code>void printCities()
  {
      ifstream city ;
      city.open("cities.txt", fstream::in);
      if(city.is_open())
      {
          char *currCity;
          int counter = 1;
          while(readNextString(city , currCity))
          {
              cout &lt;&lt; counter++ &lt;&lt; ". " &lt;&lt; currCity &lt;&lt; endl;
              delete[] currCity;
              currCity = NULL;
          }
          if(city.eof())
              cout &lt;&lt; "There are no cities added.\n";
          city.close();
      }else
          cout &lt;&lt; "Error by opening 'cities.txt'.Make sure that the file exist and try again.\n";
  }
  </code></pre>
  
  <p>Hope I was clear enough. If you find some other mistakes or possible errors I'll be glad to hear it and learn from it.</p>