why is the loop started a third time?
conff is an opened file which contains 4 lines only
Code:
short int nubses = 0; string line;
while (conff)
{
getline(conff, line); OPT[nubses] = line.substr(4, line.size());
getline(conff, line); OPT_Vuln[nubses] = line.substr(9, line.size());
nubses++;
}
but the program goes into the loop a third time and crashes... why? it should be end of file
Re: why is the loop started a third time?
I find that it is usually best to code these loops as follows:
Code:
while (getline(conff, line))
{
OPT[nubses] = line.substr(4, line.size());
getline(conff, line);
// should probably check stream state here also
OPT_Vuln[nubses] = line.substr(9, line.size());
nubses++;
}
Re: why is the loop started a third time?
failbit, badbit, and/or eofbit are not set until after an input operation fails, so you'll need to check after each call to getline to make sure that data was actually extracted from the stream.
I think the following should work:
Code:
short int nubses = 0; string line;
while (!conff.eof())
{
if (getline(conff, line))
OPT[nubses] = line.substr(4, line.size());
if (getline(conff, line))
OPT_Vuln[nubses] = line.substr(9, line.size());
nubses++;
}
EDIT: I like Philip's version better.
Re: why is the loop started a third time?