Search code examples
cstderrerrno

Redirect perror output to fprintf(stderr, " ")


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);

Solution

  • #include <errno.h>
    
    fprintf(stderr, "%s\n", strerror(errno));
    

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