You are checking for ! prefsfile.eof ()
but if you checked for the more generic prefsfile.good()
your loop should break if the line is longer than BUFSIZE
.
The getline reference explains when the failbit is set. In particular, if the delimiter is not found by the time BUFSIZE
characters have been read from the input.
I would read the file like this:
void read (const char *filename)
{
ifstream prefsfile (filename);
if (prefsfile.is_open ())
{
char prefsline [BUFSIZE], prefname [BUFSIZE];
unsigned int value;
while (prefsfile.getline(prefsline, BUFSIZE))
{
if (prefsfile.eof()) {
break;
}
istringstream iss (prefsline);
iss >> prefname >> value;
cout << prefname << " " << value << "\n";
}
prefsfile.close ();
}
}
If the line is too long, the while
loop condition will evaluate to false.
Test 1 - long line breaks the loop
ballcolor 10
really really long line that is over 30 characters
bgcolor 5
output:
ballcolor 10
Test 2 - shorter lines
ballcolor 10
bgcolor 5
output:
ballcolor 10
bgcolor 5