背景

error是一个包含在<errno.h>中的预定义的外部int变量,用于表示最近一个函数调用是否产生了错误。若为0,则无错误,其它值均表示一类错误。

errno只在函数返回错误时才有意义,当函数执行正常返回时并不会重置errno,因此此时的errno是不能作为错误处理的。所以在有些时候需要清理error。

perror()strerrot()函数可以把errno的值转化为有意义的字符输出。

#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <errno.h>
#include <string.h> int main(void){ opendir("/tmp/no_exist");
//用法1:直接读取errno的值
printf("errno = %d\n", errno); //用法2:使用perror函数,把error的值转化为有意义的字符提示。
if(errno != 0){
perror("opendir: ");
// exit(EXIT_FAILURE);
}
// 补充一个可以 格式化打印的
fprintf(stderr, "[%s] %s\n", "opendir", strerror(errno)); //用法3:类似用法2,但使用strerror函数。
if(errno != 0){
printf("%s\n", strerror(errno));
exit(0);
} exit(0);
}

输出结果为:

errno = 2
opendir: : No such file or directory
No such file or directory

以下程序用于显示所有的错误代码含义

#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h> int main(void){ for(int i = 0; i < 140; i++){
printf("%4d:%s\n", i, strerror(i));
} return 0;
}

输出结果如下:

   0:Success
1:Operation not permitted
2:No such file or directory
3:No such process
4:Interrupted system call
5:Input/output error
6:No such device or address
7:Argument list too long
8:Exec format error
9:Bad file descriptor
10:No child processes
11:Resource temporarily unavailable
12:Cannot allocate memory
13:Permission denied
14:Bad address
15:Block device required
16:Device or resource busy
17:File exists
18:Invalid cross-device link
19:No such device
20:Not a directory
21:Is a directory
22:Invalid argument
23:Too many open files in system
24:Too many open files
25:Inappropriate ioctl for device
26:Text file busy
27:File too large
28:No space left on device
29:Illegal seek
30:Read-only file system
31:Too many links
32:Broken pipe
33:Numerical argument out of domain
34:Numerical result out of range
35:Resource deadlock avoided
36:File name too long
37:No locks available
38:Function not implemented
39:Directory not empty
40:Too many levels of symbolic links
41:Unknown error 41
42:No message of desired type
43:Identifier removed
44:Channel number out of range
45:Level 2 not synchronized
46:Level 3 halted
47:Level 3 reset
48:Link number out of range
49:Protocol driver not attached
50:No CSI structure available
51:Level 2 halted
52:Invalid exchange
53:Invalid request descriptor
54:Exchange full
55:No anode
56:Invalid request code
57:Invalid slot
58:Unknown error 58
59:Bad font file format
60:Device not a stream
61:No data available
62:Timer expired
63:Out of streams resources
64:Machine is not on the network
65:Package not installed
66:Object is remote
67:Link has been severed
68:Advertise error
69:Srmount error
70:Communication error on send
71:Protocol error
72:Multihop attempted
73:RFS specific error
74:Bad message
75:Value too large for defined data type
76:Name not unique on network
77:File descriptor in bad state
78:Remote address changed
79:Can not access a needed shared library
80:Accessing a corrupted shared library
81:.lib section in a.out corrupted
82:Attempting to link in too many shared libraries
83:Cannot exec a shared library directly
84:Invalid or incomplete multibyte or wide character
85:Interrupted system call should be restarted
86:Streams pipe error
87:Too many users
88:Socket operation on non-socket
89:Destination address required
90:Message too long
91:Protocol wrong type for socket
92:Protocol not available
93:Protocol not supported
94:Socket type not supported
95:Operation not supported
96:Protocol family not supported
97:Address family not supported by protocol
98:Address already in use
99:Cannot assign requested address
100:Network is down
101:Network is unreachable
102:Network dropped connection on reset
103:Software caused connection abort
104:Connection reset by peer
105:No buffer space available
106:Transport endpoint is already connected
107:Transport endpoint is not connected
108:Cannot send after transport endpoint shutdown
109:Too many references: cannot splice
110:Connection timed out
111:Connection refused
112:Host is down
113:No route to host
114:Operation already in progress
115:Operation now in progress
116:Stale file handle
117:Structure needs cleaning
118:Not a XENIX named type file
119:No XENIX semaphores available
120:Is a named type file
121:Remote I/O error
122:Disk quota exceeded
123:No medium found
124:Wrong medium type
125:Operation canceled
126:Required key not available
127:Key has expired
128:Key has been revoked
129:Key was rejected by service
130:Owner died
131:State not recoverable
132:Operation not possible due to RF-kill
133:Memory page has hardware error
134:Unknown error 134
135:Unknown error 135
136:Unknown error 136
137:Unknown error 137
138:Unknown error 138
139:Unknown error 139

Linux 环境编程:errno的基本用法的更多相关文章

  1. Linux环境编程相关的文章

    Linux环境编程相关的文章 好几年没有接触Linux环境下编程了,好多东西都有点生疏了.趁着现在有空打算把相关的一些技能重拾一下,顺手写一些相关的文章加深印象. 因为不是写书,也受到许多外部因素限制 ...

  2. linux环境编程(1): 实现一个单元测试框架

    写在前面 在开发的过程中,大多数人都需要对代码进行测试.目前对于c/c++项目,可以采用google的gtest框架,除此之外在github上搜索之后可以发现很多其他类似功能的项目.但把别人的轮子直接 ...

  3. Linux 环境编程:dirfd参数 有关解析

    背景 在Unix环境编程中,系统提供了很多以at结尾的函数,如openat.fstatat等,而这类函数通常有一个特点,就是形参列表中多了int dirfd 例如: int open(const ch ...

  4. linux环境编程(2): 使用pipe完成进程间通信

    1. 写在前面 linux系统内核为上层应用程序提供了多种进程间通信(IPC)的手段,适用于不同的场景,有些解决进程间数据传递的问题,另一些则解决进程间的同步问题.对于同样一种IPC机制,又有不同的A ...

  5. Linux环境编程--waitpid与fork与execlp

    waitpid waitpid(等待子进程中断或结束) 表头文件 #include<sys/types.h> #include<sys/wait.h> 定义函数 pid_t w ...

  6. Linux环境编程进程间通信机制理解

    一.Linux系统调用主要函数 二.创建进程 1.创建子进程系统调用fork() 2.验证fork()创建子进程效果 3.系统调用fork()与挂起系统调用wait() 三.模拟进程管道通信 四.pi ...

  7. Linux环境编程导引

    计算机系统硬件组成 总线 贯穿整个系统的一组电子管道称为总线, 分为: 片内总线 系统总线 数据总线DB 地址总线AB 控制总线CB 外部总线 I/O设备 I/O设备是系统与外界联系的通道 键盘鼠标是 ...

  8. 【Linux环境编程】获取网卡的实时网速

    在windows以下.我们能够看到360或者是qq安全卫士的"安全球".上面显示实时的网速情况.那么在linux里面怎样获取网卡的实时网速?事实上原理非常easy,读取须要获取网速 ...

  9. 笔记整理:计算CPU使用率 ----linux 环境编程 从应用到内核

    linux 提供time命令统计进程在用户态和内核态消耗的CPU时间: [root@localhost ~]# time sleep real 0m2.001s user 0m0.001s sys 0 ...

  10. linux环境编程(3): 使用POSIX IPC完成进程间通信

    1. 写在前面 之前的文章总结了使用管道进行进程间通信的方法,除了pipe和fifo,Linux内核还为我们提供了其他更高级的IPC方式,包括共享内存,消息队列,信号量等,本篇文章会通过一个具有完整逻 ...

随机推荐

  1. Multisim 14.0 安装教程

    1.  安装环境: win 7 64bit + Multisim 14.0 教育版. 2. 安装步骤: step 1:下载安装包NI_Circuit_Design_Suite_14.0***,解压后如 ...

  2. 文件上传--php user.ini详解

    文件上传 参考文档:https://www.php.net/manual/zh/configuration.file.per-user.php 如果你的 PHP 以模块化运行在 Apache 里,则用 ...

  3. InternLM2 Demo实操-书生浦语大模型实战营第二期第2节作业&大语言模型3

    大语言模型-3.InternLM2 Demo实操 书生浦语大模型实战营第二期第二节作业 本文包括第二期实战营的第2课作业的相关内容.本来是想在学习笔记中给InetrnLM官方教程做做补充的,没想到官方 ...

  4. Failed to download metadata for repo ‘PowerTools‘: Cannot prepare internal mirrorlist: No URLs in mi

    Failed to download metadata for repo 'AppStream' [CentOS] - Techglimpsehttps://techglimpse.com/faile ...

  5. windows版 navicat_15.0.18 安装

    视频安装地址: https://www.ghpym.com/ghvideo07.html 一.下载安装包 下载地址(百度网盘): 链接:https://pan.baidu.com/s/1MIZfmS5 ...

  6. 热烈祝贺 Splashtop 荣获“最佳远程办公解决方案”奖

    ​ 2021年2月,第十四届加拿大年度"经销商选择奖"落下帷幕.Splashtop在此次评选中荣获"最佳远程办公解决方案"奖,获得该奖项的还有微软和谷歌. 一直 ...

  7. 简易的 Linux 流量实时监控工具 watch+ifstat

    非常简单小巧的流量实时监控工具,贵在不需要安装,Linux 操作系统自带,在不出外网的环境中很是实用. watch -d ifstat eth1 如果连 ifstat 都没有的环境中也可以使用 ifc ...

  8. 高效C#编程:通过智能线程池管理提升性能

    前言 C#编程中,线程池(Thread Pool)是一个重要的概念,它允许开发者更有效地管理和利用系统资源.通过线程池,我们可以避免频繁地创建和销毁线程,从而减少系统开销并提高程序的响应速度和吞吐量. ...

  9. 微信公众号,微信小程序,百度小程序免费发放了

    小程序免费领取了,是真的免费哦 每人限领一套,100%开源,无后门 免费领取方式直接看tpframe官方网站

  10. Python读Excel数据自动化填入日常办公的网页表单

      前言 本篇内容,让你完全掌握Python是如何自动化办公的~ 一.环境准备 1.1  Python 3.7.0 1.2  Pycharm  (Python 开发工具) 1.3 Selenium  ...