errno.h头文件介绍

 

查看错误代码errno是调试程序的一个重要方法。当linux 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

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: errno.h 是 C 语言中的一个标准头文件,它包含了一些常量和宏定义,用于描述系统错误。 在执行一些操作时(如打开一个文件、创建一个线程等),可能会出现错误。如果错误发生,系统会将错误代码存储在一个全局变量 errno 中。errno.h 头文件中的常量和宏定义与这些错误代码相关。 例如,如果打开一个文件时发生错误,则可能会将 errno 设置为 ENOENT,这表示文件不存在。在 errno.h 中,它是这样定义的: ```#define ENOENT 2 /* No such file or directory */``` 你可以使用 perror 函数来打印 errno 对应的错误信息。例如: ``` if (fp == NULL) { perror("Error opening file"); } ``` 这样,如果 errno 为 ENOENT,则会输出 "Error opening file: No such file or directory"。 你还可以使用 strerror 函数来获取 errno 对应的错误信息字符串。例如: ``` if (fp == NULL) { fprintf(stderr, "Error opening file: %s\n", strerror(errno)); } ``` 这样,如果 errno 为 ENOENT,则会输出 "Error opening file: No such file or directory"。 ### 回答2: C语言的<errno.h>头文件是用来处理和报告错误的标准头文件。它定义了一个整数变量errno,用于存储最近一次发生的错误代码。 errno主要通过系统调用或库函数的返回值来判断是否发生了错误。当一个系统调用或库函数执行失败时,它通常会返回一个特殊的值,并设置errno以表示具体的错误类型。 <errno.h>头文件中定义了许多常量,表示不同类型的错误。其中一些常见的错误类型包括: - EPERM:操作不允许 - ENOENT:文件或目录不存在 - ESRCH:没有这个进程 - EINTR:被信号中断的系统调用 - EINVAL:无效的参数 - EIO:输入/输出错误 - ENOMEM:内存不足 可以使用errno全局变量来检查和报告错误。常见的做法是,在函数调用失败后,使用errno判断错误类型,并根据具体的错误类型采取相应的措施,例如重新尝试操作、输出错误信息或处理其他错误相关的操作。 具体的用法如下: 1. 在使用errno之前,应该先包含<errno.h>头文件。 2. 当一个系统调用或库函数返回一个指定的错误值时,可以通过检查errno的值来确定具体的错误类型。 3. 可以使用perror()函数来输出描述性错误信息,它会自动解释errno的值,并结合相关错误信息输出到标准错误流。 4. 如果需要清除errno的值,可以使用errno的某些特定值,例如将其置为0。 总结来说,<errno.h>头文件提供了处理和报告错误的功能,通过设置和检查errno变量的值,我们可以更容易地定位和解决C语言程序中可能出现的错误。 ### 回答3: errno.h 是 C 语言中的一个头文件,用于处理错误码(error code)。在程序运行过程中,如果发生了某种错误,C 语言会把相关的错误代码存储到全局变量 errno 中,而 errno.h 头文件则定义了一系列宏,用于表示不同的错误代码。 errno.h 头文件中的宏定义了一些常见的错误代码,比如 EDOM(数学参数超出范围)、EPERM(操作不允许)等等。这些宏通常以 E 开头,并且对应的错误代码为整数值。使用这些宏可以方便地在程序中获取和识别错误代码,并根据需要进行相应的处理。 通过包含 errno.h 头文件,我们可以使用 errno 全局变量以及相关的函数来处理错误码。errno 变量在程序开始之前会被设置为 0,表示没有错误发生。当某个函数调用失败时,它会将合适的错误码存储到 errno 中。我们可以通过检查 errno 的值来确定发生了什么错误,并根据需要采取适当的措施。 通常,在发生错误之后,我们可以使用 perror 函数来打印出与当前错误码对应的错误消息。perror 函数会自动根据 errno 的值找到对应的错误信息,并将其输出到标准错误流(stderr)。这样可以方便地定位和排查程序中出现的错误。 需要注意的是,errno.h 头文件中定义的错误码是平台特定的,也就是说在不同的操作系统中可能会略有差异。因此,在编写具有平台相关性的代码时,我们应该注意不同操作系统errno.h 头文件定义的错误码的差异。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值