su**************@yahoo.com, India said:
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.
If a read or write error occurs on a particular stream, the error indicator
for that stream will be set. See 4.9.1 of C89 or 7.19.1(2) of C99.
How to generate
those error conditions so that the code inside
if (ferror(stream))
{
// ... I should be able to test the code here
}
There is no simple answer to this question, since what constitutes a read
error or write error is very platform-specific. Assuming that your
objective is simply to find a way to test (rather than to test without
modifying the source), there is one way you could do it, which is to wrap
ferror in a customised routine which goes looking for permission from you
to generate an error, perhaps by reading a control file which contains
values describing the number of bytes that can be read from/written to the
relevant data file before an error is generated for test reasons. For
example, it might look something like this:
int f_error(FILE *stream)
{
int rc = ferror(stream);
#if DEBUG
FERROR *fe = get_pointer_to_error_testing_structure();
if(fe->bytes_read fe->bytes_okay_to_read ||
fe->bytes_written fe->bytes_okay_to_write)
{
rc = 1;
}
#endif
return rc;
}
Unfortunately, for this to work, you'd have to wrap all your reading and
writing routines, too, to maintain your test control structure's
bytes_read and bytes_written values.
Do you know what some people do? Pop the disk out half-way through the run.
Now *there's* a scientific approach! :-)
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999