Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Message text for ferror() return value

How can I get the message for error codes returned by int ferror(FILE*)? For errno there is the char * strerror(int). What is it for codes returned by ferror? If char * strerror(int) is good for that too, then I need a reference for it (I failed to find any indicator for this case).

like image 721
Notinlist Avatar asked Mar 18 '13 09:03

Notinlist


People also ask

What does Ferror return?

Return Value The ferror() function returns a nonzero value to indicate an error on the given stream . A return value of 0 means that no error has occurred. This example puts data out to a stream, and then checks that a write error has not occurred.

What does ferror do in C?

In the C Programming Language, the ferror function tests to see if the error indicator has been set for a stream pointed to by stream.

Does Ferror set errno?

Return Value If no error has occurred on stream, ferror returns 0. Otherwise, it returns a nonzero value. If stream is NULL, ferror invokes the invalid parameter handler, as described in Parameter Validation. If execution is allowed to continue, this function sets errno to EINVAL and returns 0.

What should AC function do to indicate to the caller that it has failed?

If you need to report a reason for failure, add an extra int *errorcode argument and include if (errorcode) *errorcode = reason; at the end of your function to allow callers that don't care about the reason to pass a null pointer.


2 Answers

From this reference page:

The ferror() function shall test the error indicator for the stream pointed to by stream.

This means that ferror returns a boolean that tells you if there is an error or not, nothing more. But according to the POSIX specification the value of errno will contain an error code in case of a failure.

like image 132
Some programmer dude Avatar answered Sep 24 '22 03:09

Some programmer dude


The library libexplain can return strings for the ferror. https://linux.die.net/man/3/explain_ferror From the docs:

if (ferror(fp) < 0)
{
    fprintf(stderr, "%s\n", explain_ferror(fp));
    exit(EXIT_FAILURE);
}
like image 31
stark Avatar answered Sep 24 '22 03:09

stark