errno、_doserrno、_sys_errlist 和 _sys_nerrerrno, _doserrno, _sys_errlist, and _sys_nerr

在程序执行过程中设置的保存错误代码的全局宏,以及用于显示的错误代码的等效字符串。Global macros that hold error codes that are set during program execution, and string equivalents of the error codes for display.

语法Syntax

#define errno   (*_errno())  
#define _doserrno   (*__doserrno())  
#define _sys_errlist (__sys_errlist())  
#define _sys_nerr (*__sys_nerr())  

备注Remarks

程序启动期间,errno_doserrno 均由运行时设置为 0。Both errno and _doserrno are set to 0 by the runtime during program startup. errno 是在系统级调用中发生错误时设置的。errno is set on an error in a system-level call. 由于 errno 保留设置它的上一次调用的值,因此该值可能会被后续的调用更改。Because errno holds the value for the last call that set it, this value may be changed by succeeding calls. 发生错误时设置 errno 的运行库调用不会在成功后清除 errnoRun-time library calls that set errno on an error do not clear errno on success. 始终在调用可能设置 errno 前,通过调用 _set_errno(0) 立即清除它,并在调用后立即进行检查。Always clear errno by calling _set_errno(0) immediately before a call that may set it, and check it immediately after the call.

发生错误时,errno 不必设置为与系统调用返回的错误代码相同的值。On an error, errno is not necessarily set to the same value as the error code returned by a system call. 对于 I/O 操作,_doserrno 将存储与 errno 代码等效的操作系统错误代码。For I/O operations, _doserrno stores the operating-system error-code equivalents of errno codes. 对于大多数非 I/O 操作,未设置 _doserrno 的值。For most non-I/O operations, the value of _doserrno is not set.

每个 errno 值都与可通过使用一个 perror 函数输出的 _sys_errlist 中的错误消息相关联,或者通过使用一个 strerrorstrerror_s 函数存储在字符串中。Each errno value is associated with an error message in _sys_errlist that can be printed by using one of the perror functions, or stored in a string by using one of the strerror or strerror_s functions. perrorstrerror 函数使用 _sys_errlist 数组和 _sys_nerr_sys_errlist 中的元素数量)处理错误信息。The perror and strerror functions use the _sys_errlist array and _sys_nerr—the number of elements in _sys_errlist—to process error information. 出于代码安全原因,已弃用对 _sys_errlist_sys_nerr 的直接访问。Direct access to _sys_errlist and _sys_nerr is deprecated for code-security reasons. 我们建议你使用更安全的函数版本,而非全局宏,如下所示:We recommend that you use the more secure, functional versions instead of the global macros, as shown here:

全局宏Global Macro 等效函数Functional Equivalents
_doserrno _get_doserrno_set_doserrno_get_doserrno, _set_doserrno
errno _get_errno_set_errno_get_errno, _set_errno
_sys_errlist, _sys_nerr_sys_errlist, _sys_nerr strerror_s、_strerror_s、_wcserror_s、__wcserror_sstrerror_s, _strerror_s, _wcserror_s, __wcserror_s

库数学例程通过调用 _matherr 设置 errnoLibrary math routines set errno by calling _matherr. 若要以不同方式处理数学错误,请根据 _matherr 引用说明编写你自己的例程,并将其命名为 _matherrTo handle math errors differently, write your own routine according to the _matherr reference description and name it _matherr.

下表中的所有 errno 值是在 <errno.h> 中预定义的常量,并且与 UNIX 兼容。All errno values in the following table are predefined constants in <errno.h>, and are UNIX-compatible. ISO C99 标准中仅指定了 ERANGEEILSEQEDOMOnly ERANGE, EILSEQ, and EDOM are specified in the ISO C99 standard.

常量Constant 系统错误消息System error message Value
EPERM 不允许执行该操作Operation not permitted 11
ENOENT 没有此文件或目录No such file or directory 22
ESRCH 没有此进程No such process 33
EINTR 函数中断Interrupted function 44
EIO I/O 错误I/O error 55
ENXIO 没有此设备或地址No such device or address 66
E2BIG 参数列表太长Argument list too long 77
ENOEXEC 执行格式错误Exec format error 88
EBADF 文件编号错误Bad file number 99
ECHILD 没有生成的进程No spawned processes 1010
EAGAIN 没有更多进程、没有足够内存或达到最大嵌套级别No more processes or not enough memory or maximum nesting level reached 1111
ENOMEM 没有足够内存Not enough memory 1212
EACCES 权限被拒绝Permission denied 1313
EFAULT 地址错误Bad address 1414
EBUSY 设备或资源忙碌Device or resource busy 1616
EEXIST 文件已存在File exists 1717
EXDEV 跨设备链接Cross-device link 1818
ENODEV 没有此设备No such device 1919
ENOTDIR 不是目录Not a directory 2020
EISDIR 是目录Is a directory 2121
EINVAL 参数无效Invalid argument 2222
ENFILE 系统中打开的文件太多Too many files open in system 2323
EMFILE 打开的文件太多Too many open files 2424
ENOTTY 不适当的 I/O 控制操作Inappropriate I/O control operation 2525
EFBIG 文件太大File too large 2727
ENOSPC 设备上没有剩余空间No space left on device 2828
ESPIPE 搜寻无效Invalid seek 2929
EROFS 只读文件系统Read-only file system 3030
EMLINK 链接太多Too many links 3131
EPIPE 管道损坏Broken pipe 3232
EDOM 数学参数Math argument 3333
ERANGE 结果太大Result too large 3434
EDEADLK 会发生资源死锁Resource deadlock would occur 3636
EDEADLOCK 与 EDEADLK 相同,以便与早期的 Microsoft C 版本兼容Same as EDEADLK for compatibility with older Microsoft C versions 3636
ENAMETOOLONG 文件名太长Filename too long 3838
ENOLCK 无可用锁No locks available 3939
ENOSYS 函数不受支持Function not supported 4040
ENOTEMPTY 目录不为空Directory not empty 4141
EILSEQ 非法字节序列Illegal byte sequence 4242
STRUNCATE 字符串被截断String was truncated 8080

要求Requirements

全局宏Global macro 必需的标头Required header 可选标头Optional header
errno <errno.h> 或 <stdlib.h>,<cerrno> 或 <cstdlib> (C++)<errno.h> or <stdlib.h>, <cerrno> or <cstdlib> (C++)
_doserrno, _sys_errlist, _sys_nerr_doserrno, _sys_errlist, _sys_nerr <stdlib.h>、<cstdlib> (C++)<stdlib.h>, <cstdlib> (C++) <errno.h>、<cerrno> (C++)<errno.h>, <cerrno> (C++)

_doserrno_sys_errlist_sys_nerr 宏是 Microsoft 扩展。The _doserrno, _sys_errlist, and _sys_nerr macros are Microsoft extensions. 有关更多兼容性信息,请参阅 兼容性For more compatibility information, see Compatibility.

另请参阅See Also

全局变量 Global Variables
errno 常量 errno Constants
perror、_wperror perror, _wperror
strerror、_strerror、_wcserror、__wcserror strerror, _strerror, _wcserror, __wcserror
strerror_s、_strerror_s、_wcserror_s、__wcserror_s strerror_s, _strerror_s, _wcserror_s, __wcserror_s
_get_doserrno _get_doserrno
_set_doserrno _set_doserrno
_get_errno _get_errno
_set_errno_set_errno