使用C标准库的strerror函数输出错误信息
包含头文件: string.h
函数声明:char *strerror(int errnum)
参数说明:errnum是错误号,一般使用标准库定义的全局变量errno。
返回值:返回指向错误消息字符串的指针。
功能说明:这个库函数根据errnum从内部数组里搜索错误消息字符串。消息内容取决于开发平台和编译器。
具体例子:
// 尝试打开一个不存在的文件
#include <stdio.h>
#include <string.h>
#include <errno.h>
int main () {
FILE *fp;
fp = fopen("file.txt","r");
if( fp == NULL ) {
printf("Error %d: %s\n", errno, strerror(errno));
}
return(0);
}
$ vim strerror.c
$ gcc -o strerror strerror.c
$ ./strerror
Error 2: No such file or directory
使用C标准库的perror函数输出错误信息
包含头文件: stdio.h
函数声明:void perror(const char *str)
参数说明:str是用户自己指定的,开头先打印这个字符串,后面接着打印错误消息。中间是一个冒号和空格。
返回值:无
功能说明:这个库函数打印出当前的错误消息,输出到stderr,标准错误流。
具体例子:
// 打开一个不存在的文件
#include <stdio.h>
int main () {
FILE *fp;
fp = fopen("file.txt", "r");
if( fp == NULL ) {
perror("Error");
return(-1);
}
fclose(fp);
return(0);
}
$ vim perror.c
$ gcc -o perror perror.c
$ ./perror
Error: No such file or directory
举例:
#include <errno.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
int main()
{
int a = sqrt(-1);
perror("Error");
printf("No:%d, %s\n",errno, strerror(errno));
return 0;
}
$ gcc -o test test.c -lm
$ ./test
Error: Numerical argument out of domain
No:33, Numerical argument out of domain
关于C标准中Errno的使用
errno.h是C编程语言标准库中的一个头文件。它定义了用于报告和检索错误情况的宏,使用的符号名称为errno("error number"的缩写)。
errno的作用类似于一个整数变量。当某些库函数检测到错误时,会在errno中存储一个值(错误编号)。在程序启动时,存储的值为零。库函数只存储大于零的值。任何库函数都可以在返回前改变存储的值,无论它们是否检测到错误。大多数函数通过返回一个特殊的值来表示它们检测到了错误,比如返回类型是指针的函数通返回了NULL,返回类型为整数的函数返回了-1。少数函数要求调用者将errno预设为0,并在事后对其进行测试,看是否检测到错误。
errno宏扩展为一个int类型的左值,有时还带有extern或volatile类型的指定符,这取决于平台。最初这是一个静态的内存空间,但现在几乎总是使用宏来定义,来适用于多线程,这样每个线程都会看到自己的本地线程上下文的错误号码。
头文件还定义了扩展到代表错误代码的整数常数的宏。C标准库只要求定义三个:
EDOM
Results from a parameter outside a function's domain, e.g. sqrt(-1)
ERANGE
Results from a result outside a function's range, e.g. strtol("0xfffffffff", NULL, 0) on systems with a 32-bit wide long
EILSEQ (Required since 1994 Amendment 1 to C89 standard)
Results from an illegal byte sequence, e.g. mbstowcs(buf, "\xff", 1) on systems that use UTF-8.
符合POSIX规范的操作系统,如AIX、Linux或Solaris,还包括许多其他错误值,其中许多错误值的使用频率远远高于上述错误值,如当一个文件不能被打开和读取时的EACCES错误。C++11标准增加了许多在POSIX规范中发现的错误值的定义。
传统上,Unix系统手册的第一页,名为intro(2),列出了所有errno.h的宏,但Linux不是这样,这些宏反而被列在errno(3)中 。
errno可以用strerror(定义在string.h中)或BSD的扩展sys_errlist翻译成描述性字符串。可以用perror(定义在stdio.h中)直接打印错误信息到标准错误流中。由于strerror在许多类Unix系统中不是线程安全的,所以使用了一个线程安全的版本strerror_r,但是在POSIX和GNU中定义是不一样的,使得它比sys_errlist表更难移植。
Linux内核中的errno定义
查看了Linux kernel 5.17-rc4的代码,在include文件夹内的errno相关的定义:
#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 /* Argument 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 */
/*
* This error code is special: arch syscall entry code will return
* -ENOSYS if users try to call a syscall that doesn't exist. To keep
* failures of syscalls that really do exist distinguishable from
* failures due to attempts to use a nonexistent syscall, syscall
* implementations should refrain from returning -ENOSYS.
*/
#define ENOSYS 38 /* Invalid system call number */
#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 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 */
#define ECANCELED 125 /* Operation Canceled */
#define ENOKEY 126 /* Required key not available */
#define EKEYEXPIRED 127 /* Key has expired */
#define EKEYREVOKED 128 /* Key has been revoked */
#define EKEYREJECTED 129 /* Key was rejected by service */
/* for robust mutexes */
#define EOWNERDEAD 130 /* Owner died */
#define ENOTRECOVERABLE 131 /* State not recoverable */
#define ERFKILL 132 /* Operation not possible due to RF-kill */
#define EHWPOISON 133 /* Memory page has hardware error */
/*
* These should never be seen by user programs. To return one of ERESTART*
* codes, signal_pending() MUST be set. Note that ptrace can observe these
* at syscall exit tracing, but they will never be left for the debugged user
* process to see.
*/
#define ERESTARTSYS 512
#define ERESTARTNOINTR 513
#define ERESTARTNOHAND 514 /* restart if no handler.. */
#define ENOIOCTLCMD 515 /* No ioctl command */
#define ERESTART_RESTARTBLOCK 516 /* restart by calling sys_restart_syscall */
#define EPROBE_DEFER 517 /* Driver requests probe retry */
#define EOPENSTALE 518 /* open found a stale dentry */
#define ENOPARAM 519 /* Parameter not supported */
/* Defined for the NFSv3 protocol */
#define EBADHANDLE 521 /* Illegal NFS file handle */
#define ENOTSYNC 522 /* Update synchronization mismatch */
#define EBADCOOKIE 523 /* Cookie is stale */
#define ENOTSUPP 524 /* Operation is not supported */
#define ETOOSMALL 525 /* Buffer or request is too small */
#define ESERVERFAULT 526 /* An untranslatable error occurred */
#define EBADTYPE 527 /* Type not supported by server */
#define EJUKEBOX 528 /* Request initiated, but will not complete before timeout */
#define EIOCBQUEUED 529 /* iocb queued, will get completion event */
#define ERECALLCONFLICT 530 /* conflict with recalled state */
#define ENOGRACE 531 /* NFS file lock reclaim refused */
这些errno的定义,都是正整数。
在Ubuntu中,可以如下快速查询errno信息:
$ sudo apt install moreutils
$ errno 2
ENOENT 2 No such file or directory
$ errno -l
EPERM 1 Operation not permitted
ENOENT 2 No such file or directory
ESRCH 3 No such process
EINTR 4 Interrupted system call
EIO 5 Input/output error
。。。。。。。
而在Windows中的errno的含义和Linux是不一样的。errno是Visual Studio中的C runtime library的Global constants,也要包含errno.h头文件。
并非ERRNO.H中给出的所有定义都在32位Windows操作系统中使用。ERRNO.H中的一些值是为了保持与UNIX系列操作系统的兼容性。32位Windows操作系统中的errno值是UNIX系统中errno值的一个子集。
errno值不一定与Windows操作系统的系统调用所返回的实际错误代码相同。要访问实际的操作系统错误代码,请使用_doserrno变量。
Windows支持的errno值如下:
Constant
|
Description
|
Value
|
E2BIG
|
Argument list too long.
|
7
|
EACCES
|
Permission denied. The file's permission setting does not allow the specified access. This error signifies that an attempt was made to access a file (or, in some cases, a directory) in a way that is incompatible with the file's attributes.
For example, the error can occur when an attempt is made to read from a file that is not open, to open an existing read-only file for writing, or to open a directory instead of a file. Under MS-DOS operating system versions 3.0 and later, EACCES may also indicate a locking or sharing violation.
The error can also occur in an attempt to rename a file or directory or to remove an existing directory.
|
13
|
EAGAIN
|
No more processes or not enough memory or maximum nesting level reached. An attempt to create a new process failed because there are no more process slots, or there is not enough memory, or the maximum nesting level has been reached.
|
11
|
EBADF
|
Bad file number. There are two possible causes: 1) The specified file descriptor is not a valid value or does not refer to an open file. 2) An attempt was made to write to a file or device opened for read-only access.
|
9
|
EBUSY
|
Device or resource busy.
|
16
|
ECHILD
|
No spawned processes.
|
10
|
EDEADLK
|
Resource deadlock would occur.
|
36
|
EDEADLOCK
|
Same as EDEADLK for compatibility with older Microsoft C versions.
|
36
|
EDOM
|
Math argument. The argument to a math function is not in the domain of the function.
|
33
|
EEXIST
|
Files exists. An attempt has been made to create a file that already exists. For example, the _O_CREAT and _O_EXCL flags are specified in an _open call, but the named file already exists.
|
17
|
EFAULT
|
Bad address.
|
14
|
EFBIG
|
File too large.
|
27
|
EILSEQ
|
Illegal sequence of bytes (for example, in an MBCS string).
|
42
|
EINTR
|
Interrupted function.
|
4
|
EINVAL
|
Invalid argument. An invalid value was given for one of the arguments to a function. For example, the value given for the origin when positioning a file pointer (by means of a call to fseek) is before the beginning of the file.
|
22
|
EIO
|
I/O error.
|
5
|
EISDIR
|
Is a directory.
|
21
|
EMFILE
|
Too many open files. No more file descriptors are available, so no more files can be opened.
|
24
|
EMLINK
|
Too many links.
|
31
|
ENAMETOOLONG
|
Filename too long.
|
38
|
ENFILE
|
Too many files open in system.
|
23
|
ENODEV
|
No such device.
|
19
|
ENOENT
|
No such file or directory. The specified file or directory does not exist or cannot be found. This message can occur whenever a specified file does not exist or a component of a path does not specify an existing directory.
|
2
|
ENOEXEC
|
Exec format error. An attempt was made to execute a file that is not executable or that has an invalid executable-file format.
|
8
|
ENOLCK
|
No locks available.
|
39
|
ENOMEM
|
Not enough memory is available for the attempted operator. For example, this message can occur when insufficient memory is available to execute a child process, or when the allocation request in a _getcwd call cannot be satisfied.
|
12
|
ENOSPC
|
No space left on device. No more space for writing is available on the device (for example, when the disk is full).
|
28
|
ENOSYS
|
Function not supported.
|
40
|
ENOTDIR
|
Not a directory.
|
20
|
ENOTEMPTY
|
Directory not empty.
|
41
|
ENOTTY
|
Inappropriate I/O control operation.
|
25
|
ENXIO
|
No such device or address.
|
6
|
EPERM
|
Operation not permitted.
|
1
|
EPIPE
|
Broken pipe.
|
32
|
ERANGE
|
Result too large. An argument to a math function is too large, resulting in partial or total loss of significance in the result. This error can also occur in other functions when an argument is larger than expected (for example, when the buffer argument to _getcwd is longer than expected).
|
34
|
EROFS
|
Read only file system.
|
30
|
ESPIPE
|
Invalid seek.
|
29
|
ESRCH
|
No such process.
|
3
|
EXDEV
|
Cross-device link. An attempt was made to move a file to a different device (using the rename function).
|
18
|
STRUNCATE
|
A string copy or concatenation resulted in a truncated string. See _TRUNCATE.
|
80
|
而为了和POSIX保持一致所定义的errno值:
Constant
|
Description
|
Value
|
EADDRINUSE
|
Address in use.
|
100
|
EADDRNOTAVAIL
|
Address not available.
|
101
|
EAFNOSUPPORT
|
Address family not supported.
|
102
|
EALREADY
|
Connection already in progress.
|
103
|
EBADMSG
|
Bad message.
|
104
|
ECANCELED
|
Operation canceled.
|
105
|
ECONNABORTED
|
Connection aborted.
|
106
|
ECONNREFUSED
|
Connection refused.
|
107
|
ECONNRESET
|
Connection reset.
|
108
|
EDESTADDRREQ
|
Destination address required.
|
109
|
EHOSTUNREACH
|
Host unreachable.
|
110
|
EIDRM
|
Identifier removed.
|
111
|
EINPROGRESS
|
Operation in progress.
|
112
|
EISCONN
|
Already connected.
|
113
|
ELOOP
|
Too many symbolic link levels.
|
114
|
EMSGSIZE
|
Message size.
|
115
|
ENETDOWN
|
Network down.
|
116
|
ENETRESET
|
Network reset.
|
117
|
ENETUNREACH
|
Network unreachable.
|
118
|
ENOBUFS
|
No buffer space.
|
119
|
ENODATA
|
No message available.
|
120
|
ENOLINK
|
No link.
|
121
|
ENOMSG
|
No message.
|
122
|
ENOPROTOOPT
|
No protocol option.
|
123
|
ENOSR
|
No stream resources.
|
124
|
ENOSTR
|
Not a stream.
|
125
|
ENOTCONN
|
Not connected.
|
126
|
ENOTRECOVERABLE
|
State not recoverable.
|
127
|
ENOTSOCK
|
Not a socket.
|
128
|
ENOTSUP
|
Not supported.
|
129
|
EOPNOTSUPP
|
Operation not supported.
|
130
|
EOTHER
|
Other.
|
131
|
EOVERFLOW
|
Value too large.
|
132
|
EOWNERDEAD
|
Owner dead.
|
133
|
EPROTO
|
Protocol error.
|
134
|
EPROTONOSUPPORT
|
Protocol not supported.
|
135
|
EPROTOTYPE
|
Wrong protocol type.
|
136
|
ETIME
|
Stream timeout.
|
137
|
ETIMEDOUT
|
Timed out.
|
138
|
ETXTBSY
|
Text file busy.
|
139
|
EWOULDBLOCK
|
Operation would block.
|
140
|
参考: