0

Hi I have following loop for reading in persons:

void personFin(vector<Person>* persvect, string filename)
{
    ifstream personfile(filename);
    string name;
    int lhstrength;
    int rhstrength;
    int erasiveskill;
    int ID = 1;
    cout << "reading in persons" << endl;
    if (personfile.good())
    {
        while (!(personfile >> name).eof())
        {
            cout << "start read in " << ID << endl;
            personfile >> lhstrength;
            personfile >> rhstrength;
            personfile >> erasiveskill;
            Person temp(name, lhstrength, rhstrength, erasiveskill, ID);
            persvect->push_back(temp);
            cout << "end read in " << ID << endl;
            ID++;
        }
        personfile.close();
    }
    else
    {
        cout << "ERROR: Cannot open file.\n";
    }
}

I executed my program in Xcode and everything worked fine when I compile it with g++ the input file loop is infinite

thanks for your help

David G
  • 94,763
  • 41
  • 167
  • 253
awvaz
  • 23
  • 2
  • Have you tried opening your file in binary mode? – David G Dec 13 '14 at 04:04
  • 1
    I think it's better to use `while (!(personfile >> name))` (without eof()), because a read could fail for some other reason, so failbit could be on, but eofbit off. – Zaskar Dec 13 '14 at 04:18

0 Answers0