From -1981561167533800110
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,f330f27bea8a80d1
X-Google-Attributes: gidf78e5,public
X-Google-ArrivalTime: 1994-10-12 12:33:19 PST
Newsgroups: comp.std.c++
Path: bga.com!news.sprintlink.net!howland.reston.ans.net!vixen.cso.uiuc.edu!newsrelay.iastate.edu!newsxfer.itd.umich.edu!europa.eng.gtefsd.com!MathWorks.Com!solaris.cc.vt.edu!uunet!uunet.ca!uunet.ca!cognos!jimp
From: jimp@cognos.COM (Jim Patterson)
Subject: Re: File input problem
Message-ID: <1994Oct12.151830.11243@cognos.com>
Sender: news@cognos.com (News Administrator)
Organization: Cognos Incorporated, Ottawa CANADA
X-Newsreader: TIN [version 1.2 PL2]
References: <CxH771.Czy@Virginia.EDU>
Date: Wed, 12 Oct 1994 15:18:30 GMT
Lines: 40

Gregory Carl Lewin (gcl8a@Virginia.EDU) wrote:
: I have encountered a mysterious problem while inputting files
: with a while{} loop.  In particular, when I use the following
: statements:

: 	while(inFile)
: 		{
: 		inFile.get(ch);
: 		cout<<ch;
: 		}

: the while loop seems to cycle one extra time, adding a spoace
: to the end of the line.  This happens in TC++ 3.1, BC++4.0
: (EasyWin and WinApp(with out the cout, of course)).

: Is there a fix for this?

Not likely, because I don't think it's broken. You just seem to
have misinterpreted the meaning of "infile()".

If !inFile is TRUE, this indicates that an error or failure has
happened. The failure doesn't actually happen until an attempt to
read past EOF happens; so, inFile.get is called one more time than
the actual number of characters, and the last call will fail. Your
loop is putting out a character even though the get failed, which is
why you see an extra character.

Try this:
   while (1) {
    infile.get(ch);
    if (!inFile)
	  break;
    cout<<ch;
  }

--
Jim Patterson                  Cognos Incorporated
Sr Consulting Engineer         P.O. BOX 9707
UUNET:jimp@cognos.COM          3755 Riverside Drive
PHONE:(613)738-1338 x3385      Ottawa, Ont  K1G 3Z4


