Re: Replacing blank lines in my .txt data file
Ulrich Eckhardt wrote:
[...]
The idiomatic way is this:
while( getline(in,line))
... // handle line
if(in.eof())
... // reached EOF
else
... // failed before reaching EOF
Just a detail, but the only way getline can normally fail is if
the last line is not terminated by a '\n'. And of course, this
sets EOF as well. If you want to reliably handle an incorrectly
terminated last line:
while ( in.peek() != EOF ) {
if ( ! getline( in, line ) ) {
// missing '\n'...
// If I understand correctly, line should still
// contain the characters up to the EOF...
}
// ...
}
"Abnormally", getline can fail because the streambuf raised an
exception, or because of std::bad_alloc. Both set badbit, which
can be tested with in.bad().
--
James Kanze GABI Software
Conseils en informatique orient?e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S?mard, 78210 St.-Cyr-l'?cole, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]