- errno
- strerror
在调用Linux系统API的时候,可能出现一些错误,例如使用open有些时候会返回-1。这个时候需要知道失败的原因,于是全局变量errno就相当有用了。
#include#include #include int main ( void ) { int fd; extern int errno; if ( ( fd = open ( "/dev/dsp", O_WRonLY ) ) < 0 ) { printf ( "errno = %dn", errno ); return 1; } return 0; }
如果dsp设备忙的话,errno的值将是16。
strerror可以使用strerror来翻译error:
#include#include #include #include int main ( void ) { int fd; extern int errno; if ( ( fd = open ( "/dev/dsp", O_WRonLY ) ) < 0 ) { printf ( "errno = %dn", errno ); char *mesg = strerror ( errno ); printf ( "Mesg: %sn", mesg ); } return 0; }
dsp设备忙的话,将输出如下信息:
errno = 16 Mesg: Device or resource busy



