473,382 Members | 1,784 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

EOF vs. feof() and ferror()

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
8 14360

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
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
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
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

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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
by: Mantorok Redgormor | last post by:
My professor that teaches C says that using feof to test if EOF is reached while using a function like fgets on a file is okay. but i've been told elsewhere that it is not okay. what is the case...
36
by: Stephen Howe | last post by:
Hi, If I attempt to read past the end of a file, feof() will return a non-zero value. But can I guarantee that ferror() is 0? In short, will the error indicator be set in some implementations...
8
by: Joriveek | last post by:
Hi, If I use while(!feof( FileRead )), it reads each line from my input file until it encounters an empty space or so, but when I have empty spaces within one line, it is not working, it is...
3
by: sunnyboyGuo | last post by:
hello everyone, my code is like this: #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv) { FILE *fp; char str;
20
by: ericunfuk | last post by:
If fseek() always clears EOF, is there a way for me to fread() from an offset of a file and still be able to detect EOF?i.e. withouting using fseek(). I also need to seek to an offset in the file...
1
by: Spiros Bousbouras | last post by:
/* ... */ f = fopen("some-file" , "r") ; if ( f == NULL ) { /* Exits */ } clearerr(f) ; while ( ( a=getc(f) ) != EOF ) { /* Do stuff */ } if ( ferror(f) ) {
24
by: kindrain | last post by:
the code checks whether a.txt has exact the same lines, then write different lines into b.txt Here it is: (before that ,you should creat a.txt) ---------------------- #include<stdio.h>...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.