errno , perror,strerror
1. 简介
很多系统函数在错误返回时将错误原因记录在libc定义的全局变量errno中,每种错误原因对应一个错误码。
errno在头文件errno.h中声明,是一个整型变量,所有错误码都是正整数。
然后我们可以用perror或strerror函数将errno解释成字符串
2. perror
#include <stdio.h>
void perror(const char *s);
实例:
FILE *fp = fopen("text.txt","r+");
if ( !fp )
{
perror("Open text.txt");
}
输出结果:
Open text.txt: No such file or directory
fopen返回-1并设置errno为ENOENT,紧接着perror函数读取errno的值,将ENOENT解释成字符串No such file or directory并打印,最后打印的结果是Open text.txt: No such file or directory。
3. strerror
#include <string.h> char *strerror(int errnum);
//返回值:错误码errnum所对应的字符串
实例:
FILE *fp = fopen("text.txt","r+");
if ( !fp )
{
perror("Open text.txt");
}
if (!fp )
{
printf( "errno = %d, message : %s\n", errno, strerror(errno) );
}
输出:
Open text.txt: No such file or directory
errno = 2, message : No such file or directory
4. Linux 中的errno
errno.00 is: Success
errno.01 is: Operation not permitted
errno.02 is: No such file or directory
errno.03 is: No such process
errno.04 is: Interrupted system call
errno.05 is: Input/output error
errno.06 is: No such device or address
errno.07 is: Argument list too long
errno.08 is: Exec format error
errno.09 is: Bad file descriptor
errno.10 is: No child processes
errno.11 is: Resource temporarily unavailable[资源临时不可用](连续发送数据时候回出此错,加延时)
errno.12 is: Cannot allocate memory
errno.13 is: Permission denied
errno.14 is: Bad address
errno.15 is: Block device required
errno.16 is: Device or resource busy
errno.17 is: File exists
errno.18 is: Invalid cross-device link
errno.19 is: No such device
errno.20 is: Not a directory
errno.21 is: Is a directory
errno.22 is: Invalid argument
errno.23 is: Too many open files in system
errno.24 is: Too many open files
errno.25 is: Inappropriate ioctl for device
errno.26 is: Text file busy
errno.27 is: File too large
errno.28 is: No space left on device
errno.29 is: Illegal seek
errno.30 is: Read-only file system
errno.31 is: Too many links
errno.32 is: Broken pipe[断开的管道](原因:the broken pipe error occurs if one end of the
TCP socket closes connection(using disconnect) or gets killed and the other
end tries to still write to it. An indication of a closed/terminated
connection is a return value of 0 when you try to read from that socket
using recv. After receiving such an error, if you try to still write to the
socket, your process gets sent the SIGPIPE signal which kills it.
)
errno.33 is: Numerical argument out of domain
errno.34 is: Numerical result out of range
errno.35 is: Resource deadlock avoided
errno.36 is: File name too long
errno.37 is: No locks available
errno.38 is: Function not implemented
errno.39 is: Directory not empty
errno.40 is: Too many levels of symbolic links
errno.41 is: Unknown error 41
errno.42 is: No message of desired type
errno.43 is: Identifier removed
errno.44 is: Channel number out of range
errno.45 is: Level 2 not synchronized
errno.46 is: Level 3 halted
errno.47 is: Level 3 reset
errno.48 is: Link number out of range
errno.49 is: Protocol driver not attached
errno.50 is: No CSI structure available
errno.51 is: Level 2 halted
errno.52 is: Invalid exchange
errno.53 is: Invalid request descriptor
errno.54 is: Exchange full
errno.55 is: No anode
errno.56 is: Invalid request code
errno.57 is: Invalid slot
errno.58 is: Unknown error 58
errno.59 is: Bad font file format
errno.60 is: Device not a stream
errno.61 is: No data available
errno.62 is: Timer expired
errno.63 is: Out of streams resources
errno.64 is: Machine is not on the network
errno.65 is: Package not installed
errno.66 is: Object is remote
errno.67 is: Link has been severed
errno.68 is: Advertise error
errno.69 is: Srmount error
errno.70 is: Communication error on send
errno.71 is: Protocol error
errno.72 is: Multihop attempted
errno.73 is: RFS specific error
errno.74 is: Bad message
errno.75 is: Value too large for defined data type
errno.76 is: Name not unique on network
errno.77 is: File descriptor in bad state
errno.78 is: Remote address changed
errno.79 is: Can not access a needed shared library
errno.80 is: Accessing a corrupted shared library
errno.81 is: .lib section in a.out corrupted
errno.82 is: Attempting to link in too many shared libraries
errno.83 is: Cannot exec a shared library directly
errno.84 is: Invalid or incomplete multibyte or wide character
errno.85 is: Interrupted system call should be restarted
errno.86 is: Streams pipe error
errno.87 is: Too many users
errno.88 is: Socket operation on non-socket
errno.89 is: Destination address required
errno.90 is: Message too long
errno.91 is: Protocol wrong type for socket
errno.92 is: Protocol not available
errno.93 is: Protocol not supported
errno.94 is: Socket type not supported
errno.95 is: Operation not supported
errno.96 is: Protocol family not supported
errno.97 is: Address family not supported by protocol
errno.98 is: Address already in use
errno.99 is: Cannot assign requested address
errno.100 is: Network is down
errno.101 is: Network is unreachable
errno.102 is: Network dropped connection on reset
errno.103 is: Software caused connection abort
errno.104 is: Connection reset by peer[l连接被对端重置]
errno.105 is: No buffer space available
errno.106 is: Transport endpoint is already connected
errno.107 is: Transport endpoint is not connected
errno.108 is: Cannot send after transport endpoint shutdown
errno.109 is: Too many references: cannot splice
errno.110 is: Connection timed out
errno.111 is: Connection refused
errno.112 is: Host is down
errno.113 is: No route to host
errno.114 is: Operation already in progress
errno.115 is: Operation now in progress
errno.116 is: Stale NFS file handle
errno.117 is: Structure needs cleaning
errno.118 is: Not a XENIX named type file
errno.119 is: No XENIX semaphores available
errno.120 is: Is a named type file
errno.121 is: Remote I/O error
errno.122 is: Disk quota exceeded
errno.123 is: No medium found
errno.124 is: Wrong medium type
errno.125 is: Operation canceled
errno.126 is: Required key not available
errno.127 is: Key has expired
errno.128 is: Key has been revoked
errno.129 is: Key was rejected by service
errno.130 is: Owner died
errno.131 is: State not recoverable
errno.132 is: Unknown error 132
132-255 全是Unknown error
5. windows errno
| Constant | System error message | Value |
|---|---|---|
|
EPERM |
Operation not permitted |
1 |
|
ENOENT |
No such file or directory |
2 |
|
ESRCH |
No such process |
3 |
|
EINTR |
Interrupted function |
4 |
|
EIO |
I/O error |
5 |
|
ENXIO |
No such device or address |
6 |
|
E2BIG |
Argument list too long |
7 |
|
ENOEXEC |
Exec format error |
8 |
|
EBADF |
Bad file number |
9 |
|
ECHILD |
No spawned processes |
10 |
|
EAGAIN |
No more processes or not enough memory or maximum nesting level reached |
11 |
|
ENOMEM |
Not enough memory |
12 |
|
EACCES |
Permission denied |
13 |
|
EFAULT |
Bad address |
14 |
|
EBUSY |
Device or resource busy |
16 |
|
EEXIST |
File exists |
17 |
|
EXDEV |
Cross-device link |
18 |
|
ENODEV |
No such device |
19 |
|
ENOTDIR |
Not a directory |
20 |
|
EISDIR |
Is a directory |
21 |
|
EINVAL |
Invalid argument |
22 |
|
ENFILE |
Too many files open in system |
23 |
|
EMFILE |
Too many open files |
24 |
|
ENOTTY |
Inappropriate I/O control operation |
25 |
|
EFBIG |
File too large |
27 |
|
ENOSPC |
No space left on device |
28 |
|
ESPIPE |
Invalid seek |
29 |
|
EROFS |
Read-only file system |
30 |
|
EMLINK |
Too many links |
31 |
|
EPIPE |
Broken pipe |
32 |
|
EDOM |
Math argument |
33 |
|
ERANGE |
Result too large |
34 |
|
EDEADLK |
Resource deadlock would occur |
36 |
|
EDEADLOCK |
Same as EDEADLK for compatibility with older Microsoft C versions |
36 |
|
ENAMETOOLONG |
Filename too long |
38 |
|
ENOLCK |
No locks available |
39 |
|
ENOSYS |
Function not supported |
40 |
|
ENOTEMPTY |
Directory not empty |
41 |
|
EILSEQ |
Illegal byte sequence |
42 |
|
STRUNCATE |
String was truncated |
80 |
6. 参考文献
[1] Linux C编程一站式学习
[2] Linux errno Man Page
[3] MSDN errno http://msdn.microsoft.com/zh-cn/library/vstudio/t3ayayh1%28v=vs.80%29.aspx
errno , perror,strerror的更多相关文章
- (转)linux下错误的捕获:errno和strerror的使用,以及perror和strerror的区别
经常在调用linux 系统api 的时候会出现一些错误,比方说使用open() write() creat()之类的函数有些时候会返回-1,也就是调用失败,这个时候往往需要知道失败的原因.这个时候使用 ...
- errno 与 perror()/strerror()函数
errno是个全局的int型变量,当调用一个系统函数时不管成功不成功都有可能改变errno的值.但只有不成功时errno的值才是有意义的.如果要真的想用errno进行多次打印,刚可以先将其缓存一下.下 ...
- Linux/Unix C编程之的perror函数,strerror函数,errno
#include <stdio.h> // void perror(const char *msg); #include <string.h> // char *strerro ...
- C++输出错误信息perror、strerror以及全局变量errno
头文件:#include<stdio.h>函数:perror.strerror,全局变量:errno.使用方法: FILE *fp; if((fp = fopen("test.t ...
- errno与perror
很多系统函数在错误返回时将错误原因记录在libc定义的全局变量errno中,每种错误原因对应一个错误码,请查阅errno(3)的Man Page了解各种错误码,errno在头文件errno.h中声明, ...
- 转:perror和strerror的区别
概述: perror和strerror都是C语言提供的库函数,用于获取与erno相关的错误信息,区别不大,用法也简单.最大的区别在于perror向stderr输出结果,而 strerror向stdou ...
- (九)errno和perror、标准IO
3.1.6.文件读写的一些细节3.1.6.1.errno和perror(1)errno就是error number,意思就是错误号码.linux系统中对各种常见错误做了个编号,当函数执行错误时,函数会 ...
- 进程间通信IPC:消息队列,信号量,共享内存
2015.3.4星期三 阴天 进程间通信:IPC 文件对象:记录文件描述符,文件开关等 IPC标示符:系统全局的流水号两个进程要通信,打开的是唯一的对象进行通讯,通过key操作 XSI IPC:消息队 ...
- 如何读取Linux键值,输入子系统,key,dev/input/event,dev/event,C语言键盘【转】
转自:https://blog.csdn.net/lanmanck/article/details/8423669 相信各位使用嵌入式的都希望直接读取键值,特别是芯片厂家已经提供input驱动的情况下 ...
随机推荐
- nginxhttp请求限制丶tcp会话限制和下载速度限制
(1)nginx请求限制 ngx_http_limit_req_module:开启对单个ip丶单个会话在单位时间内请求的限制rate表示限制的速率 1.修改nginx配置文件 #vim /usr/lo ...
- react native native module
React Native Native Modules,官方地址:https://facebook.github.io/react-native/docs/native-modules-android ...
- LOJ #6281. 数列分块入门 5-分块(区间开方、区间求和)
#6281. 数列分块入门 5 内存限制:256 MiB时间限制:500 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: hzwer 提交提交记录统计测试数据讨论 5 题目描述 给出 ...
- HDU 1561 The more, The Better【树形DP/有依赖的分组背包】
ACboy很喜欢玩一种战略游戏,在一个地图上,有N座城堡,每座城堡都有一定的宝物,在每次游戏中ACboy允许攻克M个城堡并获得里面的宝物.但由于地理位置原因,有些城堡不能直接攻克,要攻克这些城堡必须先 ...
- Nginx日志统一格式
统一格式如下:nginx.conf 纯文本: log_format main '$remote_addr - $remote_user [$time_local] "$request&quo ...
- Flask实战第45天:完成前台登录界面
我们的注册页面和登录页面有很多相似之处,因此,也可以基于一个模板来实现. 首先创建一个模板html,命名为front_signbase.html, 然后修改注册页面front_signup.html, ...
- 安卓 内存泄漏检测工具 LeakCanary 使用
韩梦飞沙 yue31313 韩亚飞 han_meng_fei_sha 313134555@qq.com 配置 build.gradle dependencies { debugCompile 'com ...
- bzoj 2137: submultiple
Time Limit: 10 Sec Memory Limit: 259 MB Submit: 23 ...
- 【Matrix-tree定理】【并查集】【kruscal算法】bzoj1016 [JSOI2008]最小生成树计数
题意:求一个图的最小生成树个数. 矩阵树定理:一张无向图的生成树个数 = (度数矩阵 - 邻接矩阵)的任意一个n-1主子式的值. 度数矩阵除了对角线上D[i][i]为i的度数(不计自环)外,其他位置是 ...
- 20162321 实验一 Java开发环境的熟悉(Linux + Eclipse)
北京电子科技学院(BESTI) 实 验 报 告 课程:程序设计与数据结构 班级: 1623 姓名: 王彪 学号:20162321 指导教师:娄嘉鹏 王志强 实验日期:3月25日 实验密级: 非密级 预 ...