Mail Archives: djgpp/1996/07/23/11:48:06
Gurunandan R Bhat <grbhat AT unigoa DOT ernet DOT in> wrote:
>hello,
> i need help with a problem i have with reading files created by ms-dos
>edit in djgpp v2. the relevant part of my c++ code is:
>ofstream fin(datafile);
>while (!fin.eof())
> fin >> x1 >> x2;
/IN/put from an /O/fstream???
>on i execution i find that the last line is read twice. is there some way
>i can avoid this problem?
Well, assuming that the above was a transcription error, just check
for eof() a hell of a lot, in my experience. A common problem is code
like this:
while(!fin.eof()) {
skip_whitespace(fin);
fin >> a;
fin >> b;
fin >> c;
}
If this is fed a file with trailing whitespace, it does not return eof
after the last record is read, so the loop executes again - and as
there is now an eof condition after skip_whitespace, the three fins do
nothing, leaving the same set of values in a/b/c...
Regards,
ABW
--
I have become... Comfortably numb...
Alaric B. Williams Internet : alaric AT abwillms DOT demon DOT co DOT uk
<A HREF="http://www.hardcafe.co.uk/Alaric/">http://www.hardcafe.co.uk/Alaric/</A>
- Raw text -