opinions of error functions

B

Bill Cunningham

I just wanted to get some opinions on FIlE streams and error checking
methods. I thought about EOF, NULL, and ferror. I have had trouble with EOF
so there must be something there I'm not doing right. As far as style or a
commonly used method is EOF perferred over ferror? Here's a snippet

char buf[20];
FILE *fp;
fp=fopen("file","rb+");
if(fread (buf,20,sizeof(char),fp)==EOF) {fclose(fp);}
/* I know I need error checking as well as EOF but this is a skeleton that
just opens and closes a stream */

Would it be less messy to use ferror() or NULL? Is there a perferred way?

Bill
 
K

Keith Thompson

Bill Cunningham said:
I just wanted to get some opinions on FIlE streams and error checking
methods. I thought about EOF, NULL, and ferror. I have had trouble with EOF
so there must be something there I'm not doing right. As far as style or a
commonly used method is EOF perferred over ferror? Here's a snippet

char buf[20];
FILE *fp;
fp=fopen("file","rb+");
if(fread (buf,20,sizeof(char),fp)==EOF) {fclose(fp);}
/* I know I need error checking as well as EOF but this is a skeleton that
just opens and closes a stream */

Would it be less messy to use ferror() or NULL? Is there a perferred way?

Read the documentation for the fread() function. It does *not* return
EOF to indicate an error, or for any other reason, nor does it ever
return NULL.

fread() returns a result of type size_t, which indicates the number of
items actually read. If this number doesn't match the number of items
you asked it to read, then *either* there was an error *or* it reached
the end of the file. The result returned by fread() doesn't tell you
which; you can then call feof() and/or ferror() to determine what
happened.

Unless you're absolutely 100% certain what a function does, you
shouldn't even think about trying to use it without reading the
documentation first.
 
E

Eric Sosman

Bill said:
I just wanted to get some opinions on FIlE streams and error checking
methods. I thought about EOF, NULL, and ferror. I have had trouble with EOF
so there must be something there I'm not doing right. As far as style or a
commonly used method is EOF perferred over ferror? Here's a snippet

char buf[20];
FILE *fp;
fp=fopen("file","rb+");
if(fread (buf,20,sizeof(char),fp)==EOF) {fclose(fp);}
/* I know I need error checking as well as EOF but this is a skeleton that
just opens and closes a stream */

Would it be less messy to use ferror() or NULL? Is there a perferred way?

It would be "less messy" to use a method that works ...
Consult your favorite C textbook or reference, and read
what it says about the value returned by fread(). Pay
close attention to everything the fread() description
says about the value EOF.
 
M

Martin Ambuhl

Bill said:
I just wanted to get some opinions on FIlE streams and error checking
methods. I thought about EOF, NULL, and ferror. I have had trouble with EOF
so there must be something there I'm not doing right. As far as style or a
commonly used method is EOF perferred over ferror? Here's a snippet

char buf[20];
FILE *fp;
fp=fopen("file","rb+");
if(fread (buf,20,sizeof(char),fp)==EOF) {fclose(fp);}

The above is a very bad idea.
fread() returns the number of items read. This number may be zero,
but it is _never_ less than zero. But EOF is defined as a constant
negative integral expression.
[BTW, sizeof(char) == 1 by definition, so you have a bit of typing
exercice there. You also have the size and count arguments
backwards. Your code asks for 1 item of 20 bytes. If it were not
true that sizeof(char) == 1, we could not portably know how many
items of 20 bytes you wanted.]

So let's consider
#include <stdio.h>

void /* you might change this to int and return an error code */
testread(char *buffer, size_t item_size, size_t n_items, FILE *f)
{
clearerr(f); /* make sure there are no lingering indications
of an I/O error */
if(fread(buffer, item_size, n_items, f) < n_items)
{
/* for some reason we got fewer than n_items items of size
item_size. */
if (feof(f))
{
/* we tried to read past the end of file. Handle
that condition here. */
}
if (ferror(f))
{
/* there was an error on the input stream. Handle
that condition here. */
}
}

/* I know I need error checking as well as EOF but this is a skeleton that
just opens and closes a stream */

You don't need EOF at all. In fact, you "use" is worthless.
Would it be less messy to use ferror() or NULL? Is there a perferred way?

What in heaven's name would you use NULL for?
 
B

Bill Cunningham

Martin Ambuhl said:
Bill said:
I just wanted to get some opinions on FIlE streams and error checking
methods. I thought about EOF, NULL, and ferror. I have had trouble with
EOF so there must be something there I'm not doing right. As far as style
or a commonly used method is EOF perferred over ferror? Here's a snippet

char buf[20];
FILE *fp;
fp=fopen("file","rb+");
if(fread (buf,20,sizeof(char),fp)==EOF) {fclose(fp);}

The above is a very bad idea.
fread() returns the number of items read. This number may be zero, but
it is _never_ less than zero. But EOF is defined as a constant negative
integral expression.
[BTW, sizeof(char) == 1 by definition, so you have a bit of typing
exercice there. You also have the size and count arguments
backwards. Your code asks for 1 item of 20 bytes. If it were not
true that sizeof(char) == 1, we could not portably know how many
items of 20 bytes you wanted.]

So let's consider
#include <stdio.h>

void /* you might change this to int and return an error code */
testread(char *buffer, size_t item_size, size_t n_items, FILE *f)
{
clearerr(f); /* make sure there are no lingering indications
of an I/O error */
if(fread(buffer, item_size, n_items, f) < n_items)
{
/* for some reason we got fewer than n_items items of size
item_size. */
if (feof(f))
{
/* we tried to read past the end of file. Handle
that condition here. */
}
if (ferror(f))
{
/* there was an error on the input stream. Handle
that condition here. */
}
}

/* I know I need error checking as well as EOF but this is a skeleton
that just opens and closes a stream */

You don't need EOF at all. In fact, you "use" is worthless.
Would it be less messy to use ferror() or NULL? Is there a perferred way?

What in heaven's name would you use NULL for?

I thought for error checking. THanks much Martin you answered my
question exactly as I put it. ferror() for error checking and feof() instead
of EOF. Above when you used.

if(fread(buffer, item_size, n_items, f) < n_items)

Do you need this at all? I know you are testing fread's return value so I
understand what you are trying to do. But what about this?

if(fread(buffer,item_size,n_items,f)==feof(f))
{ handle in these braces which I would probably just use
fwrite(stderr,"eof error"); exit(1) which would probably throw me into
needing the stdlib.h header.

Bill
 
K

Keith Thompson

Bill Cunningham said:
I thought for error checking. THanks much Martin you answered my
question exactly as I put it. ferror() for error checking and feof() instead
of EOF. Above when you used.

if(fread(buffer, item_size, n_items, f) < n_items)

Do you need this at all? I know you are testing fread's return value so I
understand what you are trying to do.

fread() returns a result for a reason. Use it. If it tells you it
didn't read all the items you asked for, *then* use feof() and/or
ferror() to tell you why.
But what about this?

if(fread(buffer,item_size,n_items,f)==feof(f))
{ handle in these braces which I would probably just use
fwrite(stderr,"eof error"); exit(1) which would probably throw me into
needing the stdlib.h header.

You're comparing the value returned by fread() to the value returned
by feof().

fread() returns the number of elements successfully read, as a size_t.

feof() returns an indication of whether the end-of-file indicator has
been set, either zero or non-zero.

Why would you want to compare them to each other?
(Answer: You wouldn't.)
 
B

Bill Cunningham

You're comparing the value returned by fread() to the value returned
by feof().

fread() returns the number of elements successfully read, as a size_t.

feof() returns an indication of whether the end-of-file indicator has
been set, either zero or non-zero.

Why would you want to compare them to each other?
(Answer: You wouldn't.)
Wow what I have learned tonight. This is answering some previous errors
I've been getting from the standard library. I have tried to conpare return
values of different functions just as you have said Keith and had problems.
One I get alot is concerning main not returning an int or something in my
code. I compiled with all warnings on but the code compiled. I would think
good coding even with gcc -Wall you wouldn't get warnings. Maybe I'm wrong.

Bill
 
B

Bill Reid

Keith Thompson said:
I just wanted to get some opinions on FIlE streams and error checking
methods. I thought about EOF, NULL, and ferror. I have had trouble with EOF
so there must be something there I'm not doing right. As far as style or a
commonly used method is EOF perferred over ferror? Here's a snippet

char buf[20];
FILE *fp;
fp=fopen("file","rb+");
if(fread (buf,20,sizeof(char),fp)==EOF) {fclose(fp);}
/* I know I need error checking as well as EOF but this is a skeleton that
just opens and closes a stream */

Would it be less messy to use ferror() or NULL? Is there a perferred
way?

Read the documentation for the fread() function. It does *not* return
EOF to indicate an error, or for any other reason, nor does it ever
return NULL.

Unless you're absolutely 100% certain what a function does, you
shouldn't even think about trying to use it without reading the
documentation first.

And considering the quality of most documentation, you shouldn't
even think about trying to use any foreign library functions at all...
 
P

Peter Nilsson

Keith Thompson said:
....
You're comparing the value returned by fread() to the
value returned by feof().

fread() returns the number of elements successfully
read, as a size_t.

feof() returns an indication of whether the end-of-file
indicator has been set, either zero or non-zero.

Why would you want to compare them to each other?
(Answer: You wouldn't.)

Even if you did, chances are you'd quickly find out why
it's a stupid thing to do when the first successful read
gets reported as an error! ;-)
 

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

Similar Threads

error 28
file bug 28
URGENT 1
code snippet 92
write error 13
malloc 40
fread/fwrite 2 18
comparison error 12

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top