Alex Rivera | Logout

Error checking fprintf when printing to stderr

Asked 2011-01-31T00:18:37.250
10

According to the docs, fprintf can fail and will return a negative number on failure. There are clearly many situations where it would be useful to check this value.

However, I usually use fprintf to print error messages to stderr. My code will usually look something like this:

rc = foo();
if(rc) {
  fprintf(stderr, "An error occured\n");
  //Sometimes stuff will need to be cleaned up here
  return 1;
}

In these cases, is it still possible for fprintf to fail? If so, is there anything that can be done to display the error message somehow or is there is a more reliable alternative to fprintf?

If not, is there any need to check fprintf when it is used in this way?

c
Edit
Report

1 Answer

4

You could put the error on stdout or somewhere else... At some point you just have to give error reporting a best effort and then give up.

The key is that your app "gracefully" handles it (e.g. the OS doesn't have to kill it for being bad and it tells you why it exited [if it can]).

answered 2011-01-31T00:26:01.410

Your Answer