|
-
August 11th, 2008, 12:51 PM
#1
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
-
August 11th, 2008, 01:21 PM
#2
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++;
}
-
August 11th, 2008, 01:22 PM
#3
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.
Last edited by Hermit; August 11th, 2008 at 01:25 PM.
- Alon
-
August 11th, 2008, 03:35 PM
#4
Re: why is the loop started a third time?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|