Re: getline() from string ?
On May 27, 10:38 pm, Joshua Maurice <joshuamaur...@gmail.com> wrote:
On May 27, 4:14 am, Juha Nieminen <nos...@thanks.invalid> wrote:
Joshua Maurice <joshuamaur...@gmail.com> wrote:
Actually, this is a common mistake to those who are not versed in C++
std streams. You have to check the state of the stream \after\ doing
the IO operation to know if it succeeded. The above code checks the
state, then does the IO operation, then uses the result of the
operation, which if the operation failed may be nothing, partial, etc.
There's a minor problem with that approach: It assumes that the last
line of input has an endline character. If it doesn't, it usually means
that the line will be read ok, but then eof will be immediately triggered
and thus the code might dismiss the last line as if it didn't exist at all
(because it thinks that eof was reached).
Actually no.
I think that this behavior has changed some over time; the
original standard (1998) had some definite wording problems in
its specification of getline, and different implementers may
have done different things.
Ex:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
{
ofstream fout("foo.txt", ios_base::binary);
fout.put('a');
fout.put('\n');
fout.put('b');
}
{
ifstream fin("foo.txt", ios_base::binary);
for (string line; getline(fin, line); )
cout << "read in line" << endl;
}
}
This prints out
read in line
read in line
"std::getline(std::istream& , std::string& )" (or whatever its
signature is) will read from the stream until it encounters EOF or a
newline. At that point it will stop. If no characters were read in,
including no newline character, then it sets "fail" on the stream. If
some characters were read in, including the case where it reads in a
newline and including the other case where it reaches EOF before any
newline but still reads in some character before the EOF, then it is a
success and it does not set "fail" on the stream.
That's how the 2003 standard reads.
However, you are right there is a minor nit, but it's even more minor.
Suppose we comment out the line
fout.put('b');
and run the program. Instead we see the output:
read in line
The minor nit is that the above idiom will not distinguish between
streams that end in
<some not-newline char>
and
<some not-newline char> '\n'
At least, I think that's a correct summary.
You can check for eof after the read. If there was a '\n', then
the read succeeds, and eof isn't set. Without the '\n', the
read still succeeds, but eof is set.
I find it somewhat hard to reason about streams honestly
because of the relative lack of clear and concise
documentation, and the somewhat unintuitive interface. If you
need precise "control" over what is read in, specifically you
need to distinguish this corner case, then you probably have
to manually read character by character. However, I find that
the above std::getline idiom suffices for most of the
situations I encounter.
Finally, the main point of my previous post still holds: you must do
the read operation, then check the stream state, and only then use the
result of the read operation. Do not do it in a different order
otherwise you may / will be using garbage.
And learn what the different functions concerning state mean.
The names and semantics are not very well chosen. (In
particular, it's possible for all three, good(), fail() and
bad(), to return false.)
--
James Kanze