www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/05/11/01:06:25

From: "Alexei A. Frounze" <alex DOT fru AT mtu-net DOT ru>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: feof(FILE *) NOT portable...
Date: Thu, 11 May 2000 09:27:33 +0400
Organization: MTU-Intel ISP
Lines: 68
Message-ID: <391A44C5.BA4BBCCC@mtu-net.ru>
References: <8empao$5k6$1 AT nnrp02 DOT primenet DOT com>
<390ef9f9$0$72098 AT SSP1NO17 DOT highway DOT telekom DOT at>
<8emvhq$7mn$1 AT nnrp03 DOT primenet DOT com>
<3 DOT 0 DOT 6 DOT 32 DOT 20000505015633 DOT 007b2210 AT pop DOT crosswinds DOT net> <3 DOT 0 DOT 6 DOT 32 DOT 20000510204858 DOT 007b6e40 AT pop DOT crosswinds DOT net>
NNTP-Posting-Host: ppp105-79.dialup.mtu-net.ru
Mime-Version: 1.0
X-Trace: gavrilo.mtu.ru 958022999 98707 212.188.105.79 (11 May 2000 05:29:59 GMT)
X-Complaints-To: usenet-abuse AT mtu DOT ru
NNTP-Posting-Date: 11 May 2000 05:29:59 GMT
X-Mailer: Mozilla 4.72 [en] (Win95; I)
X-Accept-Language: ru,en
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

I think the Borland C compiler you use has a bit buggy LIBC.

In fact, you must try to read one character from the file in order to
determinde EOF. Just read help from BC:
--------------------8<-----------------------
Return Value:
 _ Returns non-zero if an end-of-file
   indicator was detected on the last input
   operation on the named stream.
 _ Returns 0 if end-of-file has not been
   reached.
--------------------8<-----------------------

*Last input operation* means you must invoke feof() after fgetc() or
something like that.

Btw, you may forget about feof() almost at all if you use fgetc(). That's
because fgetc() returns an integer value (not a single-byte character). And
the return value is -1 when you try to read past the end of the file.

Good Luck
Alexei A. Frounze
-----------------------------------------
Homepage: http://alexfru.chat.ru
Mirror:   http://members.xoom.com/alexfru


"Thomas J. Hruska" wrote:
> 
> Well, after a good several hours of debugging, I finally discovered that
> feof(FILE *) does NOT work the same under DJGPP as it does under other
> compilers.  This makes me VERY bitter.  While I really doubt that Borland
> could be wrong, the possibility still exists.  I have been using some very
> important file read functions and it makes me upset that to detect the EOF
> with feof, you have to read one character past the file.  In a NORMAL
> programming language (anything other than DJGPP) you can read the last
> character, call feof(FILE *), and it will return that it has indeed reached
> the end of file.  Anyone doubt me, try this code for reading in exactly one
> line of text from a file:
> 
> char *LineInput(FILE *fp)
> {
>   char *str, x[2];
>   str = (char *)malloc(1);
>   str[0] = '\0';
>   if (feof(fp))  return str;
>   x[1] = '\0';
>   do
>   {
>     x[0] = fgetc(fp);
>     str = (char *)realloc(str, strlen(str) + 2);
>     strcat(str, x);
>   } while (!feof(fp) && x[0] != 10);
>   if (x[0] != 10)  return str;
>   str[strlen(str) - 2] = '\0';
>   str = (char *)realloc(str, strlen(str) + 1);
>   return str;
> }
> 
> Under any other compiler besides DJGPP, this works fine.  Under DJGPP, the
> last line of ANY file will have an extra character on the end.  I do NOT
> relish the idea of modifying this ancient code (wrote it in early 1997) to
> include one more realloc.  Make sure the next release of DJGPP fixes this bug.
> 
>            Thomas J. Hruska -- shinelight AT crosswinds DOT net
> Shining Light Productions -- "Meeting the needs of fellow programmers"
>                   http://www.shininglightpro.com/

- Raw text -


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