问题
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