strerror和erron参数函数的试用(经典)

strerror和erron参数函数的试用(经典)
2011年12月06日
  linux下错误的捕获:errno和strerror的使用
  经常在调用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)) 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 [b]#include [/b]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设备忙的话将输出如下:[b]errno=16Mesg:Device or resource busy[/b]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值