Question on while(!feof(fp))

W

Willem

As we all know, the following code is not quite correct:

while (!feof(fp)) { fgets(...); /* do_something */ }

And should be replaced by:

while (fgets(...)) { /* do_something */ }


But can the faulty code have the result that the loop body is *not*
called, even though there is data in the file ?

I'd say no, because there hasn't been anything yet that could have set the
end-of-file indicator, but I have a program which copies data and once in
a while, it produices an empty file.


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
R

Robert Gamble

As we all know, the following code is not quite correct:

while (!feof(fp)) { fgets(...); /* do_something */ }

And should be replaced by:

while (fgets(...)) { /* do_something */ }

But can the faulty code have the result that the loop body is *not*
called, even though there is data in the file ?

I'd say no, because there hasn't been anything yet that could have set the
end-of-file indicator, but I have a program which copies data and once in
a while, it produices an empty file.

The eof indicator for a stream is always cleared after successfully
opening a file so if no other file operations took place on the stream
before the call to feof() it couldn't return true. In the case of an
empty file, the the eof indicator wouldn't get set until a read was
attempted.
 
C

CBFalconer

Willem said:
As we all know, the following code is not quite correct:

while (!feof(fp)) { fgets(...); /* do_something */ }

And should be replaced by:

while (fgets(...)) { /* do_something */ }

Try "while (!fgets(...)) {/* do_something */}". It'll work better.
 
K

Keith Thompson

CBFalconer said:
Try "while (!fgets(...)) {/* do_something */}". It'll work better.

Um, how so? fgets() returns its first argument on success, or a null
pointer if nothing is read.
 
C

CBFalconer

Keith said:
Um, how so? fgets() returns its first argument on success, or a
null pointer if nothing is read.

Um. I assumed do_something fixed up the error. You didn't.
 
K

Keith Thompson

CBFalconer said:
Um. I assumed do_something fixed up the error. You didn't.

An odd assumption on your part, IMHO. The original question was about
while (!feof(fp)) { fgets(...); /* do_something */ }
vs.
while (fgets(...)) { /* do_something */ }
It seemed fairly obvious to me that do_something handles the line read
by fgets(); any error handling would happen after the loop.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top