关于errno有以下需要注意:

1  A common mistake is to do

if (somecall() == -1) {                printf("somecall() failed\n");                if (errno == ...) { ... }            }

where errno no longer needs to have the value it had upon return from somecall() (i.e., it may have  been  changed  by  the        printf(3)).  If the value of errno should be preserved across a library call, it must be saved:

if (somecall() == -1) {                int errsv = errno;                printf("somecall() failed\n");                if (errsv == ...) { ... }            }

It  was  common in traditional C to declare errno manually (i.e., extern int errno) instead of including <errno.h>.  Do not        do this.  It will not work with modern versions of the C library.  However, on (very) old UNIX systems,  there  may  be  no        <errno.h> and the declaration is needed.

2、如果系统调用或库函数正确执行的话,errno的值是不会被清零(置0,注意这里是不会被清零,不是不会被改变)的,假若执行函数A的时候发生了错误errno被改变,接下来直接执行函数B,如果函数B正确执行的话,errno还保留函数A发生错误时被设置的值。所以,在利用errno之前,最好先对函数的返回值进行判断,看是否发生了错误,返回值错误再利用errno判断时哪里发生了错误。所以如果一个函数无法从返回值上判断正误,而只能通过errno来判断出错,那你在调用它之前必须手动将errno清零!

3、系统调用或库函数正确执行,并不保证errno的值不会被改变!

4、任何错误号(即发生错误时errno的取值)都是非0的。

综上所述,当需要用errno来判断函数是否正确执行的时候,最好先将errno清零,函数执行结束时,通过其返回值判断函数是否正确执行,若没有正确执行,再根据errno判断时哪里发生了错误。

本篇文章来源于 Linux公社网站(www.linuxidc.com)  原文链接:http://www.linuxidc.com/Linux/2013-07/87238.htm

errno的更多相关文章

  1. ERROR 1010 (HY000): Error dropping database (can't rmdir './test/', errno: 17)

    在删除数据库的时候报标题所示错误 mysql> drop database test; ERROR (HY000): Error dropping database (can't rmdir ' ...

  2. Linux ERRNO

    摘自Linux-3.18.20的头文件include/uapi/asm-generic/errno-base.h和include/uapi/asm-generic/errno.h: #define E ...

  3. C标准头文件<errno.h>

    声明了错误处理相关的宏 errno errno即error number,在程序启动时被设为0,当某个库函数运行出现错误的时候,会将相应的能表达错误类型的数字赋值给这个左值,这些数字往往有相应的宏来表 ...

  4. C 标准库系列之errno.h

    errno.h 提供了一个整数全局变量errno,当系统调用或者库函数的错误事件发生时可能会修改该值,指明错误的原因,该值可在任何需要的地方被修改:一般情况不为0的值表示出现了异常或者错误. errn ...

  5. macosx 10.11 python pip install 出现错误OSError: [Errno 1] Operation not permitted:

    Exception: Traceback (most recent call last): File , in main status = self.run(options, args) File , ...

  6. MongoDB 由于目标计算机积极拒绝,无法连接 2014-07-25T11:00:48.634+0800 warning: Failed to connect to 127.0.0.1:27017, reason: errno:10061

    转载自:http://www.cnblogs.com/xiaoit/p/3867573.html 1:启动MongoDB 2014-07-25T11:00:48.634+0800 warning: F ...

  7. mysql errno 150

    mysql error Number 1005can't creat table'/test/#sql-640_1.frm'(errno:150)三种可能问题 外键和被引用外键类型不一样,比如inte ...

  8. 安装Cocoapods时候ERROR: While executing gem ... (Errno::EPERM)

    OS X 10.11 安装Cocoapods 出现问题的解决方法 今天尝试用 Cocoapods安装个第三方库.. 输入pod install, 发现 command not find. WTF! 估 ...

  9. SYMBDSNAP_SDK[3943]: Failed to open device: /dev/symbdsnapctl, errno: 2

    在Linux的日志/var/log/message里面看到下面错误信息: Oct 26 09:48:42 xxxxxxx SYMBDSNAP_SDK[3943]: Failed to open dev ...

随机推荐

  1. asp.net4.5尚未在web服务器上注册 解决方案

    以前都是直接用aspnet_regiis.exe -i 现在用这个不好使了,解决办法打微软补丁. https://blogs.msdn.microsoft.com/webdev/2014/11/11/ ...

  2. log4net 添加日志

    1.  在config里配置一下 <configSections>    <section name="log4net" type="System.Co ...

  3. Vim配置文件

    转载 原文网址:http://www.cnblogs.com/ma6174/archive/2011/12/10/2283393.html 花了很长时间整理的,感觉用起来很方便,共享一下. 我的vim ...

  4. Visual Studio将std::cout输出到Output窗口

    在debug的时候,输出到Output需要使用OutputDebugString函数,但部分库的log是采用std::cout输出的,需要用控制台(黑窗)程序来查看输出.有没有一种使用GUI和Outp ...

  5. 移动端下拉刷新、加载更多插件dropload.js(基于jQuery/Zepto)[转]

    使用方法 引用css和js <link rel="stylesheet" href="../dist/dropload.min.css"> < ...

  6. windows下ThinkPHP3.2.3使用memcache缓存

    准备 要使用memcache,首先要安装配置好memcache服务memcached: 下载http://downloads.northscale.com/memcached-win64-1.4.4- ...

  7. 获取youku视频下载链接(wireshark抓包分析)

    随便说两句 前两天写了一个python脚本,试图以分析网页源码的方式得到优酷视频的下载地址,结果只得到视频的纯播放地址,下载纯播放地址得到的文件也无法正常播放视频. 这里共享一下播放地址得到的方法(想 ...

  8. Denormalization

    Denormalization In computing, denormalization is the process of attempting to optimize the read perf ...

  9. Spring MVC 学习 -- 创建过程

    Spring MVC 学习 -- 创建过程 Spring MVC我们使用的时候会在web.xml中配置 <servlet> <servlet-name>SpringMVC< ...

  10. IIS上虚拟站点的web.config与主站点的web.config冲突解决方法 分类: ASP.NET 2015-06-15 14:07 60人阅读 评论(0) 收藏

    IIS上在主站点下搭建虚拟目录后,子站点中的<system.web>节点与主站点的<system.web>冲突解决方法: 在主站点的<system.web>上一级添 ...