FidoNet Echomail Archive

<<< Previous Index Next >>>

From: Paul Edwards
To: Joshua Marshall
Date: 1997-02-18 08:50:52
Subject: Re: Borland C bug ?!?

JM>>>>> while (!feof(fp))
JM>>>>> {
JM>>>>> fgets(s,500,fp);
PE>>>>    ^^^^^^^^^^^^^^^^
JM>>>>> blah blah
JM>>>>> }

PE>>>> You're not checking the return from fgets().  How will you know if
PE>>>> you hit EOF?  You have a bug.  BFN.  Paul.

JM>>> I check in the line above - feof(fp).

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

JM> Step 1: Check for EOF

Which hasn't occurred yet.

JM> Step 2: If not at EOF, read data.

And the read fails, because WHEN IT WAS TRYING TO READ THE FIRST BYTE OF

JM> Step 3: process data

Process junk.

JM> Step 4: go to Step 1.

feof() will tell you it is at EOF, just like it was when the fgets FAILED.

JM> This is what I am doing.  If I am not at EOF at the start then I will not 
JM> be at EOF when I read the data.  

You sure will be.

BTW, it's easy enough to write a test program to try this out, if you have
a file with 3 lines of data in it, how many lines does the fgets() get
called? Answer is 4.

JM> Thus I will need to read in data, since 
JM> there is some left to process.  

Not necessarily.  Why do you think fgets() returns NULL if EOF is hit
before any characters are read?  As you would have found out if you had
read the fgets() section of the ANSI standard.  Have you FREQed that yet?

JM> I cannot see how s will have junk in it as 
JM> it is terminated with a \0 at the end.

Does the ANSI standard say that it will insert a '\0' if NO characters are read?

JM> If I checked the EOF status from fgets() I may still have data that needs 
JM> to be processed!!!  


JM> If I stopped processing there I would miss it entirely.  
JM> _That_ is a bug.

I don't know what you're on about there.  How long have you been using C
for? BFN.  Paul.

 * Origin: X (3:711/934.9)
SEEN-BY: 633/267 270
@PATH: 711/934 712/624 50/99 635/728 633/267

<<< Previous Index Next >>>