How can I get error message for errno value (C language)?

青春壹個敷衍的年華 提交于 2021-02-07 11:46:14

问题


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


回答1:


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




回答2:


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


来源:https://stackoverflow.com/questions/12662765/how-can-i-get-error-message-for-errno-value-c-language

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!