delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/07/16/09:17:33

From: Vik Heyndrickx <Vik DOT Heyndrickx AT rug DOT ac DOT be>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: EOF reached AFTER EOF and not AT EOF
Date: Wed, 16 Jul 1997 12:38:23 +0200
Organization: University of Ghent, Belgium
Lines: 37
Message-ID: <33CCA49F.76D9@rug.ac.be>
References: <199707151142 DOT HAA08622 AT delorie DOT com>
NNTP-Posting-Host: eduserv1.rug.ac.be
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Finn Kettner, Post Danmark wrote:
> 
> When I use the cin.eof() function call, I will not get the propper
> answer when I have reached the end of file on stdin, but will first get
> it when I have (tried to) get() the next character AFTER the last, and
> thus have (tried to) read one character too many. If I get() the
> characters into a char variable, then the last read (the one past the
> EOF) will not impose any changes in the char variable (it keeps it's
> value).
> Is this normally DOS/WINDOWS behavier (I've stumpled on it in Access too
> - here I get a 0x00 character after the last in the file - even worse
> than no change), og am I doing something wrong???

I don't know whether this is the usual behaviour on other OS'es but it
is true that under DOS it is difficult to detect for a program (the c
library in this case) whether it is at EOF before actually at least 1
byte was tried to read beyond EOF. It's not impossible to do otherwise
but this would require to determine the length upon opening of the file.
And this would not work for text files, nor for files that are opened
for writing (CMIIAW).

> I know that I can fix this by using if(cin.get(char)) but this is not
> generally a good idea, as I would then have read one character too many,
> when I haven't reached EOF (and have to put that character back into the
> buffer).

A solution: it is not obliged to store the last character gotten when
you are certain it is beyond EOF. Put it into a variable and after it
has been assertained that it is valid, do with it what a man has got to
do with it (I watch too many soaps)

I hope that I might have helped you a little.

-- 
+----------------+
| Vik Heyndrickx |
+----------------+

- Raw text -


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