|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
On Jan 30, 1:27 pm, Willem <wil...@stack.nl> 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 */ } > > 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 |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
CBFalconer <cbfalconer@yahoo.com> writes:
> 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) <kst-u@mib.org> Nokia "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister" |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
Keith Thompson wrote:
> CBFalconer <cbfalconer@yahoo.com> writes: >> Willem wrote: >> .... snip ... >>> >>> 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. Um. I assumed do_something fixed up the error. You didn't. -- [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 |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
CBFalconer <cbfalconer@yahoo.com> writes:
> Keith Thompson wrote: >> CBFalconer <cbfalconer@yahoo.com> writes: >>> Willem wrote: >>> > ... snip ... >>>> >>>> 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. > > 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. -- Keith Thompson (The_Other_Keith) <kst-u@mib.org> Nokia "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister" |
|
![]() |
| Outils de la discussion | |
|
|