I
Ioannis Vranos
The FAQ at 15.5 mentions:
http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.5
---------- From the FAQ ----------
Because the eof state may not get set until after a read is attempted past the end of file. That is, reading
the last byte from a file might not set the eof state. E.g., suppose the input stream is mapped to a keyboard
— in that case it's not even theoretically possible for the C++ library to predict whether or not the
character that the user just typed will be the last character.
For example, the following code might have an off-by-one error with the count i:
int i = 0;
while (! std::cin.eof()) { // WRONG! (not reliable)
std::cin >> x;
++i;
// Work with x ...
}
What you really need is:
int i = 0;
while (std::cin >> x) { // RIGHT! (reliable)
++i;
// Work with x ...
}
----------------------------------
At the last while condition, when the last character is read, EOF will not have been read, so the condition
will not evaluate to true, variable i will be incremented, and object x will be processed.
Then, when EOF will be read, the condition while (std::cin >> x) will still not evaluate to true, and variable
i will be incremented again, and object x will be processed again.
Then, when EOF will be read again, the condition while (std::cin >> x) will evaluate to true.
Therefore, I think the last code should be corrected to:
int i = 0;
while (std::cin >> x and not std::cin.eof()) { // RIGHT! (reliable)
++i;
// Work with x ...
}
or better to:
int i = 0;
while (std::cin.good()) { // RIGHT! (reliable)
std::cin >> x
++i;
// Work with x ...
}
--
Ioannis A. Vranos
C95 / C++03 Developer
http://www.cpp-software.net
http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.5
---------- From the FAQ ----------
Because the eof state may not get set until after a read is attempted past the end of file. That is, reading
the last byte from a file might not set the eof state. E.g., suppose the input stream is mapped to a keyboard
— in that case it's not even theoretically possible for the C++ library to predict whether or not the
character that the user just typed will be the last character.
For example, the following code might have an off-by-one error with the count i:
int i = 0;
while (! std::cin.eof()) { // WRONG! (not reliable)
std::cin >> x;
++i;
// Work with x ...
}
What you really need is:
int i = 0;
while (std::cin >> x) { // RIGHT! (reliable)
++i;
// Work with x ...
}
----------------------------------
At the last while condition, when the last character is read, EOF will not have been read, so the condition
will not evaluate to true, variable i will be incremented, and object x will be processed.
Then, when EOF will be read, the condition while (std::cin >> x) will still not evaluate to true, and variable
i will be incremented again, and object x will be processed again.
Then, when EOF will be read again, the condition while (std::cin >> x) will evaluate to true.
Therefore, I think the last code should be corrected to:
int i = 0;
while (std::cin >> x and not std::cin.eof()) { // RIGHT! (reliable)
++i;
// Work with x ...
}
or better to:
int i = 0;
while (std::cin.good()) { // RIGHT! (reliable)
std::cin >> x
++i;
// Work with x ...
}
--
Ioannis A. Vranos
C95 / C++03 Developer
http://www.cpp-software.net