Mail Archives: djgpp/2000/05/10/21:12:59
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 -