经常在调用Linux 系统api 的时候会出现一些错误,比方说使用open() write() creat()之类的函数有些时候会返回-1,也就是调用失败,这个时候往往需要知道失败的原因。这个时候使用errno这个全局变量就相当有用了。

在程序代码中包含 #include,然后每次程序调用失败的时候,系统会自动用用错误代码填充errno这个全局变量,这样你只需要读errno这个全局变量就可以获得失败原因了。

例如:

#include

#include

#include

int main(void)

{

int   fd;

extern int errno;

if((fd =open("/dev/dsp",O_WRONLY)) < 0)

{

printf("errno=%d\n",errno);

}

exit(0);

}

如果dsp设备忙的话errno值将是16。

errno.h中定义的错误代码值如下:

查 看错误代码errno是调试程序的一个重要方法。当linuc C api函数发生异常时,一般会将errno变量(需include errno.h)赋一个整数值,不同的值表示不同的含义,可以通过查看该值推测出错的原因。在实际编程中用这一招解决了不少原本看来莫名其妙的问题。比较 麻烦的是每次都要去linux源代码里面查找错误代码的含义,现在把它贴出来,以后需要查时就来这里看了。

以下来自linux 2.4.20-18的内核代码中的/usr/include/asm/errno.h

#ifndef _I386_ERRNO_H

#define _I386_ERRNO_H

#define EPERM   1 /* Operation not permitted */

#define ENOENT   2 /* No such file or directory */

#define ESRCH   3 /* No such process */

#define EINTR   4 /* Interrupted system call */

#define EIO       5 /* I/O error */

#define ENXIO   6 /* No such device or address */

#define E2BIG   7 /* Arg list too long */

#define ENOEXEC   8 /* Exec format error */

#define EBADF   9 /* Bad file number */

#define ECHILD 10 /* No child processes */

#define EAGAIN 11 /* Try again */

#define ENOMEM 12 /* Out of memory */

#define EACCES 13 /* Permission denied */

#define EFAULT 14 /* Bad address */

#define ENOTBLK 15 /* Block device required */

#define EBUSY 16 /* Device or resource busy */

#define EEXIST 17 /* File exists */

#define EXDEV 18 /* Cross-device link */

#define ENODEV 19 /* No such device */

#define ENOTDIR 20 /* Not a directory */

#define EISDIR 21 /* Is a directory */

#define EINVAL 22 /* Invalid argument */

#define ENFILE 23 /* File table overflow */

#define EMFILE 24 /* Too many open files */

#define ENOTTY 25 /* Not a typewriter */

#define ETXTBSY 26 /* Text file busy */

#define EFBIG 27 /* File too large */

#define ENOSPC 28 /* No space left on device */

#define ESPIPE 29 /* Illegal seek */

#define EROFS 30 /* Read-only file system */

#define EMLINK 31 /* Too many links */

#define EPIPE 32 /* Broken pipe */

#define EDOM 33 /* Math argument out of domain of func */

#define ERANGE 34 /* Math result not representable */

#define EDEADLK 35 /* Resource deadlock would occur */

#define ENAMETOOLONG 36 /* File name too long */

#define ENOLCK 37 /* No record locks available */

#define ENOSYS 38 /* Function not implemented */

#define ENOTEMPTY 39 /* Directory not empty */

#define ELOOP 40 /* Too many symbolic links encountered */

#define EWOULDBLOCK EAGAIN /* Operation would block */

#define ENOMSG 42 /* No message of desired type */

#define EIDRM 43 /* Identifier removed */

#define ECHRNG 44 /* Channel number out of range */

#define EL2NSYNC 45 /* Level 2 not synchronized */

#define EL3HLT 46 /* Level 3 halted */

#define EL3RST 47 /* Level 3 reset */

#define ELNRNG 48 /* Link number out of range */

#define EUNATCH 49 /* Protocol driver not attached */

#define ENOCSI 50 /* No CSI structure available */

#define EL2HLT 51 /* Level 2 halted */

#define EBADE 52 /* Invalid exchange */

#define EBADR 53 /* Invalid request descriptor */

#define EXFULL 54 /* Exchange full */

#define ENOANO 55 /* No anode */

#define EBADRQC 56 /* Invalid request code */

#define EBADSLT 57 /* Invalid slot */

#define EDEADLOCK EDEADLK

#define EBFONT 59 /* Bad font file format */

#define ENOSTR 60 /* Device not a stream */

#define ENODATA 61 /* No data available */

#define ETIME 62 /* Timer expired */

#define ENOSR 63 /* Out of streams resources */

#define ENONET 64 /* Machine is not on the network */

#define ENOPKG 65 /* Package not installed */

#define EREMOTE 66 /* Object is remote */

#define ENOLINK 67 /* Link has been severed */

#define EADV 68 /* Advertise error */

#define ESRMNT 69 /* Srmount error */

#define ECOMM 70 /* Communication error on send */

#define EPROTO 71 /* Protocol error */

#define EMULTIHOP 72 /* Multihop attempted */

#define EDOTDOT 73 /* RFS specific error */

#define EBADMSG 74 /* Not a data message */

#define EOVERFLOW 75 /* Value too large for defined data type */

#define ENOTUNIQ 76 /* Name not unique on network */

#define EBADFD 77 /* File descriptor in bad state */

#define EREMCHG 78 /* Remote address changed */

#define ELIBACC 79 /* Can not access a needed shared library */

#define ELIBBAD 80 /* Accessing a corrupted shared library */

#define ELIBSCN 81 /* .lib section in a.out corrupted */

#define ELIBMAX 82 /* Attempting to link in too many shared libraries */

#define ELIBEXEC 83 /* Cannot exec a shared library directly */

#define EILSEQ 84 /* Illegal byte sequence */

#define ERESTART 85 /* Interrupted system call should be restarted */

#define ESTRPIPE 86 /* Streams pipe error */

#define EUSERS 87 /* Too many users */

#define ENOTSOCK 88 /* Socket operation on non-socket */

#define EDESTADDRREQ 89 /* Destination address required */

#define EMSGSIZE 90 /* Message too long */

#define EPROTOTYPE 91 /* Protocol wrong type for socket */

#define ENOPROTOOPT 92 /* Protocol not available */

#define EPROTONOSUPPORT 93 /* Protocol not supported */

#define ESOCKTNOSUPPORT 94 /* Socket type not supported */

#define EOPNOTSUPP 95 /* Operation not supported on transport endpoint */

#define EPFNOSUPPORT 96 /* Protocol family not supported */

#define EAFNOSUPPORT 97 /* Address family not supported by protocol */

#define EADDRINUSE 98 /* Address already in use */

#define EADDRNOTAVAIL 99 /* Cannot assign requested address */

#define ENETDOWN 100 /* Network is down */

#define ENETUNREACH 101 /* Network is unreachable */

#define ENETRESET 102 /* Network dropped connection because of reset */

#define ECONNABORTED 103 /* Software caused connection abort */

#define ECONNRESET 104 /* Connection reset by peer */

#define ENOBUFS 105 /* No buffer space available */

#define EISCONN 106 /* Transport endpoint is already connected */

#define ENOTCONN 107 /* Transport endpoint is not connected */

#define ESHUTDOWN 108 /* Cannot send after transport endpoint shutdown */

#define ETOOMANYREFS 109 /* Too many references: cannot splice */

#define ETIMEDOUT 110 /* Connection timed out */

#define ECONNREFUSED 111 /* Connection refused */

#define EHOSTDOWN 112 /* Host is down */

#define EHOSTUNREACH 113 /* No route to host */

#define EALREADY 114 /* Operation already in progress */

#define EINPROGRESS 115 /* Operation now in progress */

#define ESTALE 116 /* Stale NFS file handle */

#define EUCLEAN 117 /* Structure needs cleaning */

#define ENOTNAM 118 /* Not a XENIX named type file */

#define ENAVAIL 119 /* No XENIX semaphores available */

#define EISNAM 120 /* Is a named type file */

#define EREMOTEIO 121 /* Remote I/O error */

#define EDQUOT 122 /* Quota exceeded */

#define ENOMEDIUM 123 /* No medium found */

#define EMEDIUMTYPE 124 /* Wrong medium type */

#endif

同时也可以使用strerror()来自己翻译

如:

#include

#include

#include

int main(void)

{

int   fd;

extern int errno;

if((fd =open("/dev/dsp",O_WRONLY)) < 0)

{

printf("errno=%d\n",errno);

char * mesg = strerror(errno);

printf("Mesg:%s\n",mesg);

}

exit(0);

}

dsp设备忙的话将输出如下:

errno=16

Mesg:Device or resource busy

linux错误 翻译,Linux下错误的捕获:errno和strerror的使用相关推荐

  1. linux c代码出现段错误,Linux下段错误(C语言)

    问题描述:在Linux下编程有时会出现段错误的提醒,出现这种错误有可能是因为以下几种原因 1.数组越界:如果在初始化或者接收输入时内容超过了定义好的数组元素个数时会出现段错误,Linux的数组越界检查 ...

  2. 在linux下 用户的密码错误,linux中root用户密码错误如何解决

    linux中root用户密码错误如何解决 发布时间:2020-04-27 10:53:03 来源:亿速云 阅读:230 作者:小新 今天小编给大家分享的是linux中root用户密码错误如何解决,相信 ...

  3. linux 中的代码如何编译错误提示,Ubuntu 下GCC编译代码错误提示没有system.h和conio.h,如何得到这两个文件...

    mudaizi12345 于 2015-01-23 23:42:50发表: very good mudaizi12345 于 2015-01-23 20:36:00发表: 路过试一试 菜菜123 于 ...

  4. linux翻译成中文,快速更正linux的翻译错误

    我们总是希望GUI的程序都是简体中文的.繁体中文也能够接受.最无奈的就是满屏幕的E文了. 我们常常希望GUI的翻译是最专业准确的.用词生硬尚能够看懂.最无语的就是半中半英了. 前几天,更新了amaro ...

  5. linux 命令自动提示错误信息,Linux基础命令---sar显示系统活动信息

    选项说明 -A显示所有报告 -b显示IO状态以及传输速率,有一下值可以显示: tps,每秒发送给物理设备的传输总数.传输是对物理设备的I/O请求.多个逻辑请求可以组合成对设备的单个I/O请求.转移是不 ...

  6. linux 的错误输出黑洞,Linux的标准输出、标准错误输出、nohup

    1.在bash中标准输出可以用1来表示:通常来说这个1可以省略: 如./xxx >/dev/null 和 ./xxx 1>/dev/null 是一个意思 2.在bash中标准错误输出可以用 ...

  7. linux网络重置报错,Linux网络编程中EAGAIN错误和EINTR错误

    在Linux环境下开发经常会碰到很多错误(设置errno),其中EAGAIN是其中比较常见的一个错误(比如用在非阻塞操作中). 从字面上来看,是提示再试一次.这个错误经常出现在当应用程序进行一些非阻塞 ...

  8. linux的make提示错误46,Linux 2.6.21编译发生错误

    CHK     include/linux/version.h CHK     include/linux/utsrelease.h HOSTCC  scripts/mod/sumversion.o ...

  9. linux nginx 安装出错,Linux Nginx安装以及可能出现错误

    Linux Nginx安装以及可能出现错误 转载请标明出处 http://coderknock.com安装过程 从 http://nginx.org/download/nginx-1.9.15.tar ...

最新文章

  1. leetcode算法题--等差数列划分
  2. pycharm中使用anaconda中python环境
  3. BeautifulSoup 一行代码获取今日日期,与smtplib结合
  4. 软件缺陷的优先级和严重性定义
  5. 使用一阶微分对图像锐化
  6. spring事务管理-Transaction模板(了解)
  7. 怎么设置php的css颜色代码,CSS的文本字体颜色如何设置
  8. 用实例分析H264 RTP payload
  9. MongoDB 安装记录
  10. 九章基础算法03:树和递归
  11. 使用MVC框架中要注意的问题(一):修改首页以支持主题
  12. java 下划线_Java SE 9:“ _”(下划线)更改
  13. Spring Cloud入门教程-Ribbon实现客户端负载均衡
  14. 图像语义分割(3)-Dilated Conv:使用空洞卷积进行多尺度语义聚合
  15. 企业邮箱能设置个人昵称吗,如何设置?
  16. 无线WIFI WPS认证机制破解
  17. ios6.0_6.1_苹果手机_evasion_完美越狱
  18. java 解析umd文件_Webpack UMD:严重依赖...无法静态提取
  19. 怎样把音频文件转换成mp3格式?
  20. 网站访问量统计 | hexo

热门文章

  1. 中国EDONG网(中国排名第16的IDC)的技术力量和他的24小时客服。
  2. 求职(国企招聘渠道)
  3. 凭证预制时行项目增加成本中心描述列
  4. 创维E900V22C/E900V22D_S905L3(L3B)_安卓9.0_通刷卡刷固件
  5. 《Python Web开发实战》踩地雷记17/3/24
  6. Jetpack全家桶(一)之简介
  7. iMovie教程:如何给视频进行防抖动处理?
  8. Linux 修改 .bashrc
  9. 科技新品 | 佳能多款数码复合机;索泰科技显卡和迷你电脑新成员;莱维特立体声麦克风组...
  10. 谷歌技术团队出品,Android Flutter全家桶学习资料【全新版】