By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,058 Members | 1,209 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,058 IT Pros & Developers. It's quick & easy.

EOF vs. feof() and ferror()

P: n/a
rCs
Which of the following two approaches is preferred (and why)?

int c;

do {
...
c = getchar();
} while (c != EOF);

- or -

int c;

do {
...
c = getchar();
} while (!feof(stdin) && !ferror(stdin));

rCs

Oct 31 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a

rCs wrote:
Which of the following two approaches is preferred (and why)?

int c;

do {
...
c = getchar();
} while (c != EOF);

- or -

int c;

do {
...
c = getchar();
} while (!feof(stdin) && !ferror(stdin));

Neither - it's rarely appropriate to use a do ... while() loop in this
context. I can't imagine what processing your loop body would do before
reading the first character.

feof() is (IMHO) rarely needed, as it's normally possible to recognise
EOF directly.

int c;
while((c = getchar()) != EOF) {
....
}

would probably be my approach in all but the most obscure cases.

Oct 31 '06 #2

P: n/a
On 31 Oct 2006 05:36:41 -0800, mark_bluemel@... wrote:
>feof() is (IMHO) rarely needed, as it's normally possible to recognise
EOF directly.

int c;
while((c = getchar()) != EOF) {
...
}

would probably be my approach in all but the most obscure cases.
Ok, but after the loop you need to distinguish between EOF and error,
e.g. with feof() or ferror().

Best wishes,
Roland Pibinger
Oct 31 '06 #3

P: n/a
On 31 Oct 2006 05:36:41 -0800, in comp.lang.c , ma**********@pobox.com
wrote:
>feof() is (IMHO) rarely needed, as it's normally possible to recognise
EOF directly.
Depends on the context. There's a FAQ about the use of feof().

--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Oct 31 '06 #4

P: n/a
2006-10-31 <11**********************@k70g2000cwa.googlegroups .com>,
ma**********@pobox.com wrote:
feof() is (IMHO) rarely needed, as it's normally possible to recognise
EOF directly.
the point of feof() is to find out _why_ you got EOF.
Oct 31 '06 #5

P: n/a

rCs wrote:
Which of the following two approaches is preferred (and why)?

int c;

do {
...
c = getchar();
} while (c != EOF);

- or -

int c;

do {
...
c = getchar();
} while (!feof(stdin) && !ferror(stdin));

rCs
Neither option's that good, but of the two the first one is preferable.
You always want to test the value returned from getchar() for EOF,
rather than relying on feof() to tell you when you've hit the
end-of-file. feof() won't return true until *after* you've tried to
read beyond the end of the file, so you wind up looping once too often.
I'd rewrite it as

while ((c = getchar() ) != EOF)
{
/* do something with c */
}

if (feof(stdin))
{
/* handle end-of-file condition */
}
else
{
/* handle error condition */
}

Oct 31 '06 #6

P: n/a
John Bode wrote:
rCs wrote:
>Which of the following two approaches is preferred (and why)?

int c;

do {
...
c = getchar();
} while (c != EOF);

- or -

int c;

do {
...
c = getchar();
} while (!feof(stdin) && !ferror(stdin));

rCs

Neither option's that good, but of the two the first one is preferable.
You always want to test the value returned from getchar() for EOF,
rather than relying on feof() to tell you when you've hit the
end-of-file. feof() won't return true until *after* you've tried to
read beyond the end of the file, so you wind up looping once too often.
Both options given by rCs will loop the same number of times. In fact,
their behaviour is equivalent. When getchar() has returned EOF, then one
of feof(stdin) or ferror(stdin) must be true. The only cost is the time
spent making two additional function calls per loop, which is likely to
be negligible.

You could say that both of them loop once too often. That is on the
first time through the loop, when c is uninitialised.
I'd rewrite it as

while ((c = getchar() ) != EOF)
{
/* do something with c */
}
This is better merely because it avoids the first time through the loop
when c was uninitialised.
if (feof(stdin))
{
/* handle end-of-file condition */
}
else
{
/* handle error condition */
}
rCs's code would still have to perform this test or equivalent, after
the loop finished.

--
Simon.
Nov 1 '06 #7

P: n/a
On 31 Oct 2006 14:37:15 -0800, "John Bode" <jo*******@my-deja.com>
wrote:
>
rCs wrote:
>Which of the following two approaches is preferred (and why)?

int c;

do {
...
c = getchar();
} while (c != EOF);

- or -

int c;

do {
...
c = getchar();
} while (!feof(stdin) && !ferror(stdin));

rCs

Neither option's that good, but of the two the first one is preferable.
You always want to test the value returned from getchar() for EOF,
rather than relying on feof() to tell you when you've hit the
end-of-file. feof() won't return true until *after* you've tried to
read beyond the end of the file, so you wind up looping once too often.
If c is set to EOF by getchar, then either feof or ferror must return
true. If c is not set to EOF by getchar, then both must return false.
Ergo, the two while clauses are logically equivalent.
Remove del for email
Nov 1 '06 #8

P: n/a
On 31 Oct 2006 05:27:44 -0800, "rCs" <rc*@cert.orgwrote:
>Which of the following two approaches is preferred (and why)?

int c;

do {
...
c = getchar();
} while (c != EOF);

- or -

int c;

do {
...
c = getchar();
} while (!feof(stdin) && !ferror(stdin));
The two while clauses are logically equivalent but the first avoids
two function calls (only one when EOF is finally detected) and
possibly an extra comparison (until EOF is detected).
Remove del for email
Nov 1 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.