FidoNet Echomail Archive
aust_c_here

<<< Previous Index Next >>>

From: Joshua Marshall
To: Paul Edwards
Date: 1997-02-20 18:35:04
Subject: Re: Borland C bug ?!?

Hello Paul,
	Before I get flamed at, I must apologise.

 PE>> Like I say, you have a bug.  !feof(fp) will be true, because you
 PE>> haven't hit EOF, fgets() will return NULL, because it reaches EOF when
 PE>> reading the line, and you ignore that return code, and use s
 PE>> regardless.  s will have junk in it.

--8<-- CUT LAST REPLY --8<--

I wrote a quick program:

#include <stdio.h>

int main()
{
  FILE *fptr;
  char s[100];
  char *t;
  int f;

  fptr=fopen("main.c", "r");
  if (fptr)
  {
    while (t=fgets(s, 80, fptr))
    {
      s[80]=0;
      printf("%s", s);
      printf("fgets %d\n", *t);
      printf("feof  %d\n", !feof(fptr));
    }
    printf("fgets %d\n", *t);
    printf("feof  %d\n", !feof(fptr));

    rewind(fptr);
    printf("\nREWIND\n");
    while (f=(!feof(fptr)))
    {
      t=fgets(s, 80, fptr);
      s[80]=0;
      printf("%s", s);
      printf("fgets %d\n", *t);
      printf("feof  %d\n", f);
    }
    t=fgets(s, 80, fptr);
    printf("fgets %d\n", *t);
    printf("last %d\n", *s);
    printf("feof  %d\n", f);
  }
  fclose(fptr);
  return 0;
}
/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/

Now I look at its output:

#include <stdio.h>
fgets 35
feof  1
--8<--  BITS CUT OUT --8<
fgets 32
feof  1
}
fgets 125
feof  1
/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!fgets
47 feof  1
!!*/fgets 33
feof  0
fgets 0
feof  0

REWIND
#include <stdio.h>
fgets 35
feof  1
--8<--  BITS CUT OUT --8<
fgets 32
feof  1
}
fgets 125
feof  1
/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!fgets
47 feof  1
!!*/fgets 33
feof  1
fgets 0
last 33
feof  0


As can be seen, the feof() will trap the end of file before the fgets does.
 You are right, s will have junk in it if I go past the eof, but the feof
will detect it earlier.

I still can't find why my code doesn't work correctly when compiled under
BC4.0  I cut out the bits and they worked :(.  Only when in the whole
source did they fail.  Anyway I've put the code as above using feof() to
check for eof so everything should work fine.

 Josh Marshall   Fidonet   3:640/201.14
  ->MEMBER<-     Internet  joshua{at}st.net.au
  Team Amiga     Amiganet  41:400/869.14

... Is evil a child of the nature or nurture of the beast?
--- Mail Manager 1.22x/p #1219
 * Origin: Borderline Technologies (3:640/201.14{at}fidonet)
SEEN-BY: 633/267 270
@PATH: 640/201 201 820 712/624 50/99 635/728 633/267



<<< Previous Index Next >>>