Author |
Message |
Jens Baade #1 / 9
|
 EOF
If I read a chunk of data using a loop and getc() how can I tell the difference been an error and the successful reading of all available data? I mean EOF is returned on ERROR and it's not sure that the 'cause of this error was that there was no data left to read. I think it would be much better if EOF would actually be returned at END OF FILE and not on ERROR. Jens
|
Sat, 26 Jun 2004 16:58:05 GMT |
|
 |
Zoran Cutur #2 / 9
|
 EOF
Quote: > If I read a chunk of data using a loop and getc() how > can I tell the difference been an error and the > successful reading of all available data? I mean EOF > is returned on ERROR and it's not sure that the 'cause > of this error was that there was no data left to read. > I think it would be much better if EOF would actually > be returned at END OF FILE and not on ERROR.
If you read from a file with getc() and got a EOF return from getc() you should be checking for why this value has been returned. As you say it could be that your file reached End-Of-File, than the feof() function will return a nonzero value. If there was some sort of error on the filehandle in question, you can test with ferror(). --
"LISP is worth learning for the profound enlightenment experience you will have when you finally get it; that experience will make you a better programmer for the rest of your days." -- Eric S. Raymond
|
Sat, 26 Jun 2004 17:10:59 GMT |
|
 |
Jens Baade #3 / 9
|
 EOF
Quote:
> > If I read a chunk of data using a loop and getc() how > > can I tell the difference been an error and the > > successful reading of all available data? I mean EOF > > is returned on ERROR and it's not sure that the 'cause > > of this error was that there was no data left to read. > > I think it would be much better if EOF would actually > > be returned at END OF FILE and not on ERROR. > If you read from a file with getc() and got a EOF return > from getc() you should be checking for why this value > has been returned. As you say it could be that your file > reached End-Of-File, than the feof() function will return > a nonzero value. If there was some sort of error on the > filehandle in question, you can test with ferror().
Thanks. Jens
|
Sat, 26 Jun 2004 17:26:59 GMT |
|
 |
Dave Vandervi #4 / 9
|
 EOF
Quote:
>If I read a chunk of data using a loop and getc() how >can I tell the difference been an error and the >successful reading of all available data? I mean EOF >is returned on ERROR and it's not sure that the 'cause >of this error was that there was no data left to read. >I think it would be much better if EOF would actually >be returned at END OF FILE and not on ERROR.
feof and ferror will tell you whether EOF was returned because of an error condition (ferror returns nonzero) or an actual end-of-file (feof returns nonzero). Their use is usually discouraged because they tend to be used incorrectly (they aren't useful for determining whether (for example) we've reached the end of the file, since feof doesn't return true until _after_ a read has failed because of EOF), not because they're inherently useless or Evil. This is exactly what feof is for. You've got an opportunity to use feof for the right reason, so get wild, and use it. dave --
_This_ is exactly what casts are for. You've got a once-in-a-lifetime opportunity to use a cast for the right reason, so get wild, and use it. --Richard Bos in comp.lang.c
|
Sat, 26 Jun 2004 18:14:46 GMT |
|
 |
Mike Wahle #5 / 9
|
 EOF
Quote: > If I read a chunk of data using a loop and getc() how > can I tell the difference been an error and the > successful reading of all available data? I mean EOF > is returned on ERROR and it's not sure that the 'cause > of this error was that there was no data left to read. > I think it would be much better if EOF would actually > be returned at END OF FILE and not on ERROR.
In the context of i/o 'error' means 'cannot read any more characters'. So 'EOF' qualifies for such a condition. You can distinguish between 'EOF' and a 'real' error with 'ferror()' and/or 'feof()': while(fgetc(fp) != EOF) { // do stuff Quote: }
printf("%s\n", feof(fp) ? "Reached end of file" : "Error reading file"); -Mike
|
Mon, 28 Jun 2004 11:03:07 GMT |
|
 |
Lawrence Kir #6 / 9
|
 EOF
Quote:
>>If I read a chunk of data using a loop and getc() how >>can I tell the difference been an error and the >>successful reading of all available data? I mean EOF >>is returned on ERROR and it's not sure that the 'cause >>of this error was that there was no data left to read. >>I think it would be much better if EOF would actually >>be returned at END OF FILE and not on ERROR. >feof and ferror will tell you whether EOF was returned because of an >error condition (ferror returns nonzero) or an actual end-of-file (feof >returns nonzero). Their use is usually discouraged because they tend >to be used incorrectly
I'd say that ferror() isn't used enough. Certainly the first thing you should test is the return value of the read function. After that if it indicates failure then you should test ferror() to see whether an erro occurred. Quote: >(they aren't useful for determining whether (for >example) we've reached the end of the file, since feof doesn't return >true until _after_ a read has failed because of EOF), not because they're >inherently useless or Evil.
You can use feof() for that as long as you call it after the read function and not before. However testing the return value of the function should still be the first step and after you have tested ferror() you typically know by elimination whether end-of-file has been encountered. There are issues e.g. functions like fread() can encounter end-of-file but still return data. Quote: >This is exactly what feof is for. You've got an opportunity to use feof >for the right reason, so get wild, and use it.
It is better to use ferror(). The standard dosn't exclude the possibility of both the error and end-of-file indicators being set for a stream. If that happens it is more important to know that an error has occurred. So feof() is commonly misused, but I don't think that is true for ferror() which isn't used enough. -- -----------------------------------------
-----------------------------------------
|
Thu, 01 Jul 2004 22:19:27 GMT |
|
 |
Lawrence Kir #7 / 9
|
 EOF
On Wednesday, in article
Quote:
>> If I read a chunk of data using a loop and getc() how >> can I tell the difference been an error and the >> successful reading of all available data? I mean EOF >> is returned on ERROR and it's not sure that the 'cause >> of this error was that there was no data left to read. >> I think it would be much better if EOF would actually >> be returned at END OF FILE and not on ERROR. >In the context of i/o 'error' means 'cannot read any >more characters'. So 'EOF' qualifies for such a >condition. You can distinguish between 'EOF' and >a 'real' error with 'ferror()' and/or 'feof()': >while(fgetc(fp) != EOF) >{ > // do stuff >} >printf("%s\n", feof(fp) ? "Reached end of file" > : "Error reading file");
But ferror() is better. printf("%s\n", ferror(fp) ? "Error reading file" : "Reached end of file"); If an error occurs you want to know about it irrespecive of whether you have reached the end of the file or not. -- -----------------------------------------
-----------------------------------------
|
Thu, 01 Jul 2004 22:28:08 GMT |
|
 |
Mike Wahle #8 / 9
|
 EOF
Quote: > On Wednesday, in article
> >> If I read a chunk of data using a loop and getc() how > >> can I tell the difference been an error and the > >> successful reading of all available data? I mean EOF > >> is returned on ERROR and it's not sure that the 'cause > >> of this error was that there was no data left to read. > >> I think it would be much better if EOF would actually > >> be returned at END OF FILE and not on ERROR. > >In the context of i/o 'error' means 'cannot read any > >more characters'. So 'EOF' qualifies for such a > >condition. You can distinguish between 'EOF' and > >a 'real' error with 'ferror()' and/or 'feof()': > >while(fgetc(fp) != EOF) > >{ > > // do stuff > >} > >printf("%s\n", feof(fp) ? "Reached end of file" > > : "Error reading file"); > But ferror() is better. > printf("%s\n", ferror(fp) ? "Error reading file" > : "Reached end of file"); > If an error occurs you want to know about it irrespecive of whether you > have reached the end of the file or not.
Um, can some other 'error' condition coexist with that of 'eof' ? -Mike
|
Tue, 06 Jul 2004 04:34:31 GMT |
|
 |
Mark McIntyr #9 / 9
|
 EOF
On Thu, 17 Jan 2002 12:34:31 -0800, "Mike Wahler" Quote:
>> printf("%s\n", ferror(fp) ? "Error reading file" >> : "Reached end of file"); >> If an error occurs you want to know about it irrespecive of whether you >> have reached the end of the file or not. >Um, can some other 'error' condition coexist with that >of 'eof' ?
sure, you could have no memory left to store the read data into, for example. or the file permissions could get changed as you try to read past end of file, -- Mark McIntyre CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
|
Tue, 06 Jul 2004 07:09:51 GMT |
|
|