468,496 Members | 1,801 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,496 developers. It's quick & easy.

Question on while(!feof(fp))


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
Jan 30 '08 #1
3 9798
On Jan 30, 1:27 pm, Willem <wil...@stack.nlwrote:
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.

--
Robert Gamble
Jan 30 '08 #2
Willem wrote:
>
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.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
--
Posted via a free Usenet account from http://www.teranews.com

Jan 31 '08 #3
CBFalconer <cb********@yahoo.comwrites:
Willem wrote:
>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.
Um, how so? fgets() returns its first argument on success, or a null
pointer if nothing is read.

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jan 31 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Mohammed Mazid | last post: by
3 posts views Thread by Stevey | last post: by
10 posts views Thread by glenn | last post: by
53 posts views Thread by Jeff | last post: by
56 posts views Thread by spibou | last post: by
2 posts views Thread by Allan Ebdrup | last post: by
3 posts views Thread by gieforce | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.