[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

question on ferror() function

subramanian100in@yahoo.com, India

8/11/2008 5:38:00 AM

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
8 Answers

Ian Collins

8/11/2008 5:48:00 AM

0

subramanian100in@yahoo.com, India wrote:
> 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.

--
Ian Collins.

Richard Heathfield

8/11/2008 6:02:00 AM

0

subramanian100in@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....
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/goog...
"Usenet is a strange place" - dmr 29 July 1999

CBFalconer

8/11/2008 10:47:00 PM

0

"subramanian100in@yahoo.com, India" wrote:
>
> 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.welco...
<http://c-fa... (C-faq)
<http://benpfaff.org/writings/clc/off-topi...
<http://www.open-std.org/jtc1/sc22/wg14/www/docs/n12... (C99)
<http://cbfalconer.home.att.net/download/n869_t... (C99, txt)
<http://www.dinkumware.com/c9... (C-library}
<http://gcc.gnu.org/onlin... (GNU docs)
<http://clc-wiki.net/wiki/C_community:comp.lang.c:Introd...

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.a...
Try the download section.


Ian Collins

8/12/2008 5:29:00 AM

0

CBFalconer wrote:
> "subramanian100in@yahoo.com, India" wrote:
>> 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.
>
Which is rather vague on this issue.

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


--
Ian Collins.

Richard Heathfield

8/12/2008 6:07:00 AM

0

Ian Collins said:

> CBFalconer wrote:
>> "subramanian100in@yahoo.com, India" wrote:
>>> 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.
>>
> Which is rather vague on this issue.

And in any case doesn't answer the question "how can I test code that is
wrapped in if(ferror(fp))?".

> Richard's answer is correct according to standard,

Aye...

> mine was a hint at some thing to try.

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?

--
Richard Heathfield <http://www.cpax....
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/goog...
"Usenet is a strange place" - dmr 29 July 1999

gordonb.6p6kz

8/12/2008 6:25:00 AM

0

>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.

For testing purposes, there are a number of things you can do for
testing purposes, some suggested elsewhere in this thread:

- Ensure that the disk fills up and has no more free space.
- Remove the disk while the program is running.
- Use floppies with bad sectors.
- Disconnect the power cable to the drive while the program is running.
- Use a remote file system and disconnect the network cable, shut down
the server, or use your firewall to shut off network connectivity
temporarily.

Ian Collins

8/12/2008 8:16:00 AM

0

Richard Heathfield wrote:
> 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 );
}

--
Ian Collins.

Richard Heathfield

8/12/2008 9:03:00 AM

0

Ian Collins said:

> Richard Heathfield wrote:
>> 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().

Ooooh you're a bad lad, Ian! :-)

>
> On less accommodating systems [...]
>
> The error path could be tested with:
>
> int main(void)
> {
> FILE* f = fopen( "someFile", "w" );
>
> char buf;
>
> fread( &buf, 1, 1, f );
>
> writeFile( f );

Aha! That's what you meant - unit tests. Sorry for misunderstanding you.

Of course, this approach would not work at the integration test level -
but, depending on the breadth, depth, and scope of the unit test suite,
that might not be a problem.

--
Richard Heathfield <http://www.cpax....
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/goog...
"Usenet is a strange place" - dmr 29 July 1999