EOF vs. feof() and ferror()

R

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
 
M

mark_bluemel

rCs said:
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.
 
R

Roland Pibinger

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
 
M

Mark McIntyre

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
 
J

John Bode

rCs said:
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 */
}
 
S

Simon Biber

John said:
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.
 
B

Barry Schwarz

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
 
B

Barry Schwarz

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
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top