Redirect perror output to fprintf(stderr, " ")

RajSanpui picture RajSanpui · Mar 30, 2011 · Viewed 40.4k times · Source

In case a system call function fails, we normally use perror to output the error message. I want to use fprintf to output the perror string. How can I do something like this:

fprintf(stderr, perror output string here);

Answer

maverik picture maverik · Mar 30, 2011
#include <errno.h>

fprintf(stderr, "%s\n", strerror(errno));

Note: strerror doesn't apply \n to the end of the message