errno.h是C/C++标准库中提供的一个常用头文件,它定义了一些常用的宏定义,用于表示系统调用函数可能返回的错误码。具体来说,errno.h中定义了一些宏,它们的值就是系统调用函数可能返回的错误码。
常用宏定义
#define EINVAL 22 /* Invalid argument */ #define ERANGE 34 /* Math result not representable */ #define EAGAIN 11 /* Try again */ #define ENOTSUP 95 /* Operation is not supported */ #define ENOMEM 12 /* Not enough space */ #define EACCES 13 /* Permission denied */ #define ENOENT 2 /* No such file or directory */ #define EEXIST 17 /* File exists */ #define EBADF 9 /* Bad file number */ #define EINTR 4 /* Interrupted system call */ #define ENOSYS 38 /* Function not implemented */ #define EFAULT 14 /* Bad address */ #define EIO 5 /* I/O error */ #define EPERM 1 /* Operation not permitted */ #define ETIMEDOUT 110 /* Connection timed out */
以上就是errno.h中定义的一些常用宏定义,它们表示系统调用函数可能返回的错误码。比如,当系统调用函数返回EINVAL时,表示该函数的参数无效;当系统调用函数返回ENOMEM时,表示没有足够的内存可用;当系统调用函数返回EACCES时,表示没有足够的权限访问该函数等。
使用方法
使用errno.h中定义的宏时,需要在代码中包含errno.h头文件:
#include
可以使用errno变量来获取系统调用函数返回的错误码,并使用errno.h中定义的宏来解析该错误码:
if (errno == EINVAL) { printf("Invalid argument\n"); } else if (errno == ENOMEM) { printf("Not enough space\n"); }
可以使用perror函数来打印出错误码对应的错误消息:
perror("Error");
errno.h提供的宏定义可以帮助我们更好地理解系统调用函数可能返回的错误码,从而更好地处理系统调用函数可能出现的错误。