0

Okay, I've seen answers on here but I still don't understand exactly what I need to do. I am trying to read in stuff from a file but I'm getting unexpected results with .eof() and I'm becoming frustrated with it! I cut out the stuff I'm actually doing for a project I'm doing but hopefully the code I have is enough for help. I have a bunch of similar while loops in my functions as I'm trying to parse data from a file and assign the data to the correct data structures. It seems to work when it wants too as one file I was testing works, while another says it reaches the end of the file when in reality it has one more piece of data to read. I don't know what's going on. Thank you ahead of time for the help!

void ReadStuff(ifstream &theFile)
{
   while (!theFile.eof()) 
   {
       string line;
       getline(theFile, line);
       istringstream read(line);
       string lineSect;

       while (!read.eof())
       {
          read >> lineSect;

          if (read.eof())
          {
             break;
          }

          //Reading stuff for this line 
       }

    //Reading more stuff from a file until all data for all labels is read
    }
}
Dani
  • 322
  • 2
  • 4
  • 15
  • Don't use `while (eof())`, do `while (read >> lineSect)`. – David G Sep 26 '14 at 02:57
  • Oh my gosh! That's exactly what I needed! Thank you so much!!! :) I was freaking out since this is part of a large project that's due tomorrow morning. Out of curiosity, why does .eof() have such unexpected results if you know? It worked with some .txt files as expected but not with others. – Dani Sep 26 '14 at 03:23
  • [Here's a good explanation.](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – David G Sep 26 '14 at 03:28

0 Answers0