Is this a correct way to use ferror() ?

S

Spiros Bousbouras

/* ... */
f = fopen("some-file" , "r") ;
if ( f == NULL ) {
/* Exits */
}
clearerr(f) ;
while ( ( a=getc(f) ) != EOF ) {
/* Do stuff */
}
if ( ferror(f) ) {
/* Exits */
}

Is the call to clearerr() necessary ? If ferror() returns 0
can one be reasonably certain that the whole file has been
read ?
 
S

santosh

/* ... */
f = fopen("some-file" , "r") ;
if ( f == NULL ) {
/* Exits */
}
clearerr(f) ;
while ( ( a=getc(f) ) != EOF ) {
/* Do stuff */
}
if ( ferror(f) ) {
/* Exits */
}

Is the call to clearerr() necessary ?

I don't believe so.
If ferror() returns 0 can one be reasonably certain that the whole
file has been read ?

Within the constraints of Standard C, I think that might be a reasonable
conclusion.

I generally test explicitly with feof() as well, even if ferror()
returns false. Just a habit.
 

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,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top