delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/07/15/07:44:44

Message-Id: <199707151142.HAA08622@delorie.com>
From: "Finn Kettner, Post Danmark" <fik DOT inc AT post DOT dk>
To: DJGPP Mailing List <djgpp AT delorie DOT com>
Subject: EOF reached AFTER EOF and not AT EOF
Date: Tue, 15 Jul 1997 13:43:00 +0200
MIME-Version: 1.0

Hello gurus.

I'm sorry about this question being more of a DOS/WINDOZE question than
a GPP question, but it is primarily in GPP I would like to see the
solution :-).

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 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).

EEGGG, I hope that this question was more or less understandable, and
not too stupid :-). Maybe an example would help:

 ----starting examples program----
#include <iostream.h>

main()
{
	char kar1;

	while (!(cin.eof()))
		cout << hex << (int) kar1 << kar1 <<" a character\n";
}
 ----ending examples program----

Just feed this program with a file '<filename' and see what I mean.

Yours faithfully
Finn Kettner.

- Raw text -


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