Mail Archives: djgpp/2000/05/11/01:06:25
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 -