13

How can I get error message for errno value (C language)? For example, I can write such file (errno_messages.h):

#include <errno.h>

char* get_errno_message(void){
    switch (errno) {
    case 0:
        return "";
        break;
    case EPERM:
        return "Operation not permitted";
        break;
    case ENOENT:
        return "No such file or directory";
        break;
    case ESRCH:
        return "No such process";
        break;
        /* e.t.c. */
    default:        
        break;
    }
}

But maybe such function is exists already?

Best Regards

0

2 Answers 2

20

I think what you're looking for is strerror().

0
4

Aside of strerror(), a useful function is perror that also directly prints the error out with a given prefix. Often, you will want to do something like

int fd = open(filename, O_READ);
if (fd < 0) {
  perror(filename);
  exit(EXIT_FAILURE);
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.