473,508 Members | 2,324 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

question on ferror() function

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
Aug 11 '08 #1
8 1580
su**************@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.
Aug 11 '08 #2
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
Aug 11 '08 #3
"su**************@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.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>

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
Aug 11 '08 #4
CBFalconer wrote:
"su**************@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.
Aug 12 '08 #5
Ian Collins said:
CBFalconer wrote:
>"su**************@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.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
Aug 12 '08 #6
>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.

Aug 12 '08 #7
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.
Aug 12 '08 #8
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.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
Aug 12 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2690
by: Mark | last post by:
hello! in the interests of performing robust error checking, i'm trying to write file code along the following lines: $file = @fopen( ... ); if ($file === NULL) { // show error mesage here....
36
6296
by: Stephen Howe | last post by:
Hi, If I attempt to read past the end of a file, feof() will return a non-zero value. But can I guarantee that ferror() is 0? In short, will the error indicator be set in some implementations...
28
1603
by: Andy | last post by:
Hi, I have a homework question, can some expert help me with this. q.Write a program that reads a text file and reports the number of integers and you may assume a number is defined as one or...
3
1480
by: sunnyboyGuo | last post by:
hello everyone, my code is like this: #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv) { FILE *fp; char str;
4
1074
by: Paul M | last post by:
Hi there, i just need some clarification with using windows forms, as i have read and understood that to use a .net window form you need to create a new instance of it.. If i had 3 forms,...
22
1721
by: jobo | last post by:
Hello, I'm very new to C so please forgive my ineptitude. If I am given a file with "jfewuuj3uefi8jkw128jdmnsdf\s;'d1904" I want to capture each occurence of an integer 0-9 into an array. ...
8
14377
by: rCs | last post by:
Which of the following two approaches is preferred (and why)? int c; do { ... c = getchar(); } while (c != EOF); - or -
42
6741
by: mellyshum123 | last post by:
I need to read in a comma separated file, and for this I was going to use fgets. I was reading about it at http://www.cplusplus.com/ref/ and I noticed that the document said: "Reads characters...
1
1352
by: Spiros Bousbouras | last post by:
/* ... */ f = fopen("some-file" , "r") ; if ( f == NULL ) { /* Exits */ } clearerr(f) ; while ( ( a=getc(f) ) != EOF ) { /* Do stuff */ } if ( ferror(f) ) {
0
7129
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7333
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7398
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7061
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7502
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5637
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5057
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3208
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3194
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.