美文网首页
Linux errno 编程

Linux errno 编程

作者: louyang | 来源:发表于2017-12-19 14:45 被阅读30次

Linux操作系统中,系统调用和GNU C库函数失败时,返回值为非0 (0为成功)。errno 中的值,标志着最后一次失败的错误编号。要显示有用错误信息,常用两种方法:

  • perror() 将错误信息打印到标准错误,通常是终端上。
  • strerror() 返回一个字符串的首地址,字符串就是可读的错误信息。
# cat errno-example.c

#include <stdio.h>    //printf,perro
#include <string.h>   //strerror
#include <errno.h>    //errno
#include <fcntl.h>    //open

int main()
{
    open("/xxx/xxx/xxx", 0);
    perror("open()");
    printf("strerror(): %s\n", strerror(errno));
}
# gcc errno-example.c -o errno-example && ./errno-example
open(): No such file or directory
strerror(): No such file or directory
参考

http://man7.org/linux/man-pages/man3/errno.3.html

相关文章

网友评论

      本文标题:Linux errno 编程

      本文链接:https://www.haomeiwen.com/subject/bfibwxtx.html