How can I get error message for errno value (C language)? For example, I can write such file (errno_messages.h):
如何获取errno值(C语言)的错误消息?例如,我可以编写这样的文件(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
2 个解决方案
#2
3
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
除了strerror()之外,一个有用的函数是perror,它也直接用给定的前缀输出错误。通常,你会想要做类似的事情
int fd = open(filename, O_READ); if (fd < 0) { perror(filename); exit(EXIT_FAILURE); }
#1
#2
3
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
除了strerror()之外,一个有用的函数是perror,它也直接用给定的前缀输出错误。通常,你会想要做类似的事情
int fd = open(filename, O_READ); if (fd < 0) { perror(filename); exit(EXIT_FAILURE); }