linux下的错误捕获errno和strerror()

本文深入解析Linux系统中常见的API调用错误及其对应的错误代码,包括错误含义、解决步骤,并通过示例代码展示如何在本地打印errno对应的错误消息。旨在帮助开发者快速定位和解决API调用失败的问题。

        经常在调用linux 系统api 的时候会出现一些错误,比方说使用open() write() creat()之类的函数有些时候会返回-1,也就是调用失败,这个时候往往需要知道失败的原因。这个时候使用errno这个全局变量就相当有用了。

1 errno和strerror()函数

1.1 errno

       在程序代码中包含 #include <errno.h>,然后每次程序调用失败的时候,系统会自动用用错误代码填充errno这个全局变量,这样你只需要读errno这个全局变量就可以获得失败原因了。

1.2 strerror()函数

        头文件:<string.h>
        函数原型:char * strerror(int errnum);
        将error编码errnum转换成错误消息,通过字符串返回。

1.3 perror()函数

        头文件:<stdio.h>
        函数原型:void perror(const char * msg);
        将错误消息输出到错误输出(stderr),并换行。如果msg为空指针或者指向字符串为空,则向错误输出打印errno对应的错误消息;如果msg不为空,则向错误输出打印msg的内容后再打印冒号、空格和errno对应的错误消息。

2 errno含义

#define ESUC 0 /* Success */
#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 */

3 在本机打印errno对应错误消息

#include <string.h>
#include <errno.h>
#include <stdio.h>

int main(int argc, char * argv[]) 
{
    for(int i = 0; i < 256; ++i)
    {
        fprintf(stdout, "%s\n", 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 NFS 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       Unknown error 126

<think>首先,用户的问题是关于Linux系统中文件操作时errno=2的含义及情况分析。他们引用了两个站内引用:[1][2],这些引用提供了关于errnostrerror的通用信息,但我需要基于这些参考来回答具体问题。 关键点: - errno=2 在Linux中通常表示 "No such file or directory"。 - 这通常在尝试访问不存在的文件或目录时发生。 从引用中: - 引用[1]提到errno用于捕获错误,比如在open()、write()等API调用失败时。 - 引用[2]显示了一个示例代码,其中strerror(errno)用于获取错误描述,并提到可以打印所有errno值。 用户的具体查询:查找errno=2的情况说明。 我的回答需要: - 使用中文。 - 结构清晰,逐步解决问题。 - 保证真实可靠。 - 在行内数学表达式或独立公式中使用$...$或$$...$$格式(但在这个上下文中,可能不需要数学公式,因为主题是系统编程)。 - 生成相关问题。 - 引用段落的末尾自然地添加引用标识,如[^1]。 回答结构: 1. **解释errno=2的含义**:直接说明errno=2表示“没有这样的文件或目录”。 2. **情况分析**:描述常见场景,如文件不存在、路径错误等。 3. **如何诊断**:使用strerror或perror来获取错误描述。 4. **示例**:提供一个简单的代码示例来演示。 5. **生成相关问题**:在结尾添加相关问题。 确保格式: - 行内内容使用$...$(但可能不适用)。 - 独立公式使用$$...$$(可能不适用)。 - LaTeX语法正确。 - 中文回答。 参考用户上次的问题:用户上次可能没有明确的历史,但在这个对话中,用户的问题是直接的,所以我基于此回答。 最终回答大纲: 1. 介绍errno=2的含义。 2. 详细情况分析。 3. 如何解决或诊断。 4. 代码示例。 5. 总结。 6. 生成相关问题。 相关问题想法: 1. 如何避免errno=2错误? 2. 其他常见的errno值及其含义。 3. 在编程中如何处理errno错误? 4. 文件操作的最佳实践。 5. 使用perrorstrerror的区别。 </think> ### Linux文件操作中errno=2的含义及情况分析 在Linux系统编程中,`errno` 是一个全局变量,用于捕获系统调用(如文件操作函数)的错误代码。当系统调用失败时(例如返回-1),`errno` 会被自动设置为一个整数值,表示具体的错误原因。针对您的查询,`errno=2` 表示 **“No such file or directory”**(没有这样的文件或目录)。这通常发生在文件或目录路径无效或不存在的情况下。下面我将逐步分析其原因、常见场景及解决方法。 #### 1. **errno=2的含义** - `errno=2` 对应于标准错误代码 `ENOENT`,定义在 `<errno.h>` 头文件中。它表示系统在尝试访问文件或目录时,无法在指定路径找到目标。 - 您可以使用 `strerror(errno)` 函数来获取人类可读的错误描述。例如,执行 `strerror(2)` 会返回字符串 `"No such file or directory"`,这有助于快速诊断问题[^1][^2]。 - 在编程中,常见的文件操作函数如 `open()`, `fopen()`, `stat()` 等都可能返回此错误。 #### 2. **常见情况分析** `errno=2` 通常由以下原因引起: - **文件或目录不存在**:这是最常见的原因。例如,尝试打开一个不存在的文件: ```c int fd = open("non_existent.txt", O_RDONLY); // 如果文件不存在,errno=2 ``` - **路径错误**:路径拼写错误、大小写不匹配或路径中缺少目录。例如: - 路径 `/home/user/file.txt` 中的 `user` 目录可能不存在。 - 使用相对路径(如 `./data/file.txt`),但当前工作目录不正确。 - **权限问题导致的假象**:有时用户没有父目录的读取权限,导致系统无法检查子文件是否存在,此时也可能返回 `errno=2`(而非权限错误)。 - **符号链接损坏**:如果路径中包含指向不存在的文件的符号链接。 - **临时文件删除**:在并发环境中,文件可能在打开前被其他进程删除。 典型场景: - 在脚本或程序中调用 `open()`, `read()`, `write()` 时,路径无效。 - 使用 `exec()` 系列函数执行不存在的可执行文件。 - 文件系统挂载点错误或网络文件系统(NFS)问题。 #### 3. **如何诊断解决** - **诊断方法**: - 使用 `perror()` 或 `strerror()` 输出错误描述: ```c #include <stdio.h> #include <errno.h> #include <string.h> int main() { FILE *file = fopen("missing_file.txt", "r"); if (file == NULL) { perror("fopen failed"); // 输出: fopen failed: No such file or directory printf("Error: %s\n", strerror(errno)); // 输出: Error: No such file or directory } return 0; } ``` - 检查路径:确保路径完整且正确。使用绝对路径或验证当前工作目录(通过 `getcwd()`)。 - 验证文件存在性:在操作前使用 `access()` 或 `stat()` 检查文件状态。 - **解决方案**: - **创建文件或目录**:使用 `mkdir()` 或 `touch` 命令确保路径存在。 - **修复路径**:避免硬编码路径,使用配置文件或环境变量。 - **错误处理**:在代码中添加健壮的错误处理,例如: ```c if (fd == -1) { if (errno == ENOENT) { // 处理文件不存在的情况 } else { // 处理其他错误 } } ``` - **权限检查**:使用 `ls -l` 或 `access()` 验证目录权限。 #### 4. **总结** `errno=2` 是Linux文件操作中的常见错误,根源在于路径或文件缺失。通过结合 `strerror()` 系统日志,可以快速定位问题。在编程中,始终检查系统调用返回值并处理 `errno` 能提升代码可靠性[^1][^2]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值