Topic: File input problem


Author: jimp@cognos.COM (Jim Patterson)
Date: Wed, 12 Oct 1994 15:18:30 GMT
Raw View
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




Author: harinath@myria.cs.umn.edu (Raja Raveendran Harinath)
Date: Thu, 13 Oct 1994 15:00:16 GMT
Raw View
In article <1994Oct12.151830.11243@cognos.com> jimp@cognos.COM (Jim Patterson) writes:
>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.
>
>: Is there a fix for this?

[deleted]

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

  while (infile.get(ch)) cout.put(ch);  // or cout << ch; whichever you like

The variation of get that I have used above was included expressly
for this purpose.  The alternative would have been

  while ((ch = infile.get()) != EOF) cout << ch;

Either way, both are more compact than the other solution above.
--
---------------------------------------------------------------------
Raja R Harinath                 Graduate Student
1530 South 6th Street #C1404             Dept. of Computer Science
Minneapolis, MN 55454            University of Minnesota, Twin Cities




Author: gcl8a@Virginia.EDU (Gregory Carl Lewin)
Date: Mon, 10 Oct 1994 21:18:37 GMT
Raw View
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?

Please respond via email to:
gcl8a@virginia.edu

Thank you,

Greg Lewin
.




Author: gcl8a@Virginia.EDU (Gregory Carl Lewin)
Date: Tue, 11 Oct 1994 14:05:45 GMT
Raw View
Thank you to those who pointed me in the right direction.
Problem solved.