question on ferror() function

Discussion in 'C Programming' started by subramanian100in@yahoo.com, India, Aug 11, 2008.

  1. , India

    , India Guest

    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
     
    , India, Aug 11, 2008
    #1
    1. Advertising

  2. , India

    Ian Collins Guest

    , 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.
     
    Ian Collins, Aug 11, 2008
    #2
    1. Advertising

  3. , India

    CBFalconer Guest

    ", 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.
     
    CBFalconer, Aug 11, 2008
    #3
  4. , India

    Ian Collins Guest

    CBFalconer wrote:
    > ", 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.
     
    Ian Collins, Aug 12, 2008
    #4
  5. , India

    Ian Collins Guest

    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.
     
    Ian Collins, Aug 12, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. James Vanns
    Replies:
    7
    Views:
    7,042
    Evan Carew
    Jan 21, 2004
  2. komal
    Replies:
    6
    Views:
    1,429
    msalters
    Jan 25, 2005
  3. Stephen Howe

    ferror()

    Stephen Howe, Dec 10, 2003, in forum: C Programming
    Replies:
    36
    Views:
    1,500
    Stephen Howe
    Dec 30, 2003
  4. rCs

    EOF vs. feof() and ferror()

    rCs, Oct 31, 2006, in forum: C Programming
    Replies:
    8
    Views:
    1,756
    Barry Schwarz
    Nov 1, 2006
  5. Spiros Bousbouras

    Is this a correct way to use ferror() ?

    Spiros Bousbouras, Nov 19, 2007, in forum: C Programming
    Replies:
    1
    Views:
    397
    santosh
    Nov 19, 2007
Loading...

Share This Page