question on ferror() function

S

subramanian100in

int ferror(FILE *stream)
The function ferror tests the error indicator for the stream pointed
to by stream, returning non-zero if it is set.

Under what circumstances error indicator will be set. How to generate
those error conditions so that the code inside

if (ferror(stream))
{
// ... I should be able to test the code here
}

can be tested. I do not know how to create these error scenarios so
that the error indicator for the stream is set.

Kindly clarify.

Thanks
V.Subramanian
 
I

Ian Collins

int ferror(FILE *stream)
The function ferror tests the error indicator for the stream pointed
to by stream, returning non-zero if it is set.

Under what circumstances error indicator will be set. How to generate
those error conditions so that the code inside

if (ferror(stream))
{
// ... I should be able to test the code here
}

can be tested. I do not know how to create these error scenarios so
that the error indicator for the stream is set.
Opening a file for writing and attempting to read from it should do the
trick.
 
C

CBFalconer

int ferror(FILE *stream)
The function ferror tests the error indicator for the stream
pointed to by stream, returning non-zero if it is set.

Under what circumstances error indicator will be set. How to
generate those error conditions so that the code inside

if (ferror(stream))
{
// ... I should be able to test the code here
}

can be tested. I do not know how to create these error scenarios
so that the error indicator for the stream is set.

Kindly clarify.

The easiest way to clarify things is to read the C standard.

Some useful references about C:
<http://www.ungerhu.com/jxh/clc.welcome.txt>
<http://c-faq.com/> (C-faq)
<http://benpfaff.org/writings/clc/off-topic.html>
<http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf> (C99)
<http://cbfalconer.home.att.net/download/n869_txt.bz2> (C99, txt)
<http://www.dinkumware.com/c99.aspx> (C-library}
<http://gcc.gnu.org/onlinedocs/> (GNU docs)
<http://clc-wiki.net/wiki/C_community:comp.lang.c:Introduction>
 
I

Ian Collins

CBFalconer said:
The easiest way to clarify things is to read the C standard.
Which is rather vague on this issue.

Richard's answer is correct according to standard, mine was a hint at
some thing to try.
 
I

Ian Collins

Richard said:
And so was mine. Yours was a much simpler suggestion, but I'm at a loss to
know how you're supposed to get it to work except by deliberately breaking
the code you're trying to test. Could you explain more fully what you were
getting at, please?
Well I have to admit that on my normal development platform (Solaris) I
would take advantage of the fact that library functions are week symbols
and simply provide my own ferror().

On less accommodating systems, I had assumed the OP would have something
like:

int writeFile( FILE* stream )
{
if (ferror(stream))
{
// ... I should be able to test the code here.
//
printf( "%s\n", strerror( ferror( stream )) );
return someError;
}
// Normal processing.
//
return OK;
}

The error path could be tested with:

int main(void)
{
FILE* f = fopen( "someFile", "w" );

char buf;

fread( &buf, 1, 1, f );

writeFile( f );
}
 

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

Forum statistics

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

Latest Threads

Top