delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/07/23/11:48:06

Xref: news2.mv.net comp.os.msdos.djgpp:6315
From: alaric AT abwillms DOT demon DOT co DOT uk (Alaric B. Williams)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: help: problem with reading files
Date: Tue, 23 Jul 1996 11:13:38 GMT
Lines: 41
Message-ID: <838120319.10346.4@abwillms.demon.co.uk>
References: <Pine DOT LNX DOT 3 DOT 91 DOT 960720055550 DOT 2102A-100000 AT aditya DOT unigoa DOT ernet DOT in>
NNTP-Posting-Host: abwillms.demon.co.uk
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019