I posted over on stack overflow, and I have received this response.
as a better way to read the file.
It makes more sense to me procedurally.
http://stackoverflow.com/questions/1...ect=1#19882576
Here is the solution I was provided with (still untested.)
"Both the orginal and the modified functions are wrong: you alway need to check for successful input after trying to read something (I guess, if I eventually pass away my headstone will have that engraved...). Using in.eof() to control an input loop in general does not work!
If the line ends in a string with less than MAX_CHAR characters, the next line get ignored: you need to check if the input ends in a newline character and, if not, ignore the remaining character. If the last input ends with a newline character, you don't want to ignore characters. Also, if the line happens to end in a string with more than 100 characters, it also doesn't work. The magical constant for std::istream::ignore() to ignore as many characters as necessary is, inconveniently, spelled std::numeric_limits<std::streamsize>::max() and is declared in the header <limits>.
Basically, your loop should start with"
1 2 3 4 5 6 7 8 9 10 11
|
while (in.getline(foo, MAX_CHAR, ';')
.getline(bar, MAX_CHAR, ';')
.get(foobar, MAX_CHAR, '\n')) {
if (foobar[in.gcount() - 1] == '\n') {
foobar[in.gcount() - 1] = '\0';
}
else {
in.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
// process the inputs
}
|
"The code uses std::istream::getline() for the first two components to avoid the separate to be stored: it is sufficient if the separator is extracted and the input is stopped. For the last component std::istream::get() is used because it is necessary to verify if the last character stored was a newline. The last character stored is access using std::istream::gcount() which contains the number of characters stored by the last unformatted input function. Since the input succeeded and it would stop either when storing a newline or after storing MAX_CHAR characters, in.gcount() - 1 is always a valid index. Note, however, that the code is not tested..."
I guess it is safe to mention that, the thing that is confusing the most is..
The first instance
|
in.get(foo, MAX_CHAR, ';');
|
in the provided function make no sense to me, why it is outside the while loop. It seems it is operating on some side effect that I am unfamiliar with, and really leaves me at a loss when, I think about modifying the file to have a second ';' as a delimiter.
Still open for feedback and input.
:)