delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/05/10/21:12:59

Message-Id: <3.0.6.32.20000510204858.007b6e40@pop.crosswinds.net>
X-Sender: shinelight AT pop DOT crosswinds DOT net
X-Mailer: QUALCOMM Windows Eudora Light Version 3.0.6 (32)
Date: Wed, 10 May 2000 20:48:58 -0500
To: djgpp AT delorie DOT com
From: "Thomas J. Hruska" <shinelight AT crosswinds DOT net>
Subject: feof(FILE *) NOT portable...
In-Reply-To: <qs76hscchr2ni7eniju6e1kle25njc6f84@4ax.com>
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>
Mime-Version: 1.0
Reply-To: djgpp AT delorie DOT com
Errors-To: nobody AT delorie DOT com
X-Mailing-List: djgpp AT delorie DOT com
X-Unsubscribes-To: listserv AT delorie DOT com

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