You can use strerror()
to get a human-readable string for the error number. This is the same string printed by perror()
but it's useful if you're formatting the error message for something other than standard error output.
For example:
#include <errno.h>
#include <string.h>
/* ... */
if(read(fd, buf, 1)==-1) {
printf("Oh dear, something went wrong with read()! %s
", strerror(errno));
}
Linux also supports the explicitly-threadsafe variant strerror_r()
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…