error是一个包含在<errno.h>中的预定义的外部int变量,用于表示最近一个函数调用是否产生了错误。若为0,则无错误,其它值均表示一类错误。
perror()和strerror()函数可以把errno的值转化为有意义的字符输出。
perror()和 strerror 使用:
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <dirent.h>
#include <stdlib.h>
int main(){
extern int errno;
errno = ;
opendir("");
printf("errno %d\n", errno);
if(errno!=){
perror("opendir");
}
if(errno!=){
printf("%s\n", strerror(errno));
}
return ;
}
执行结果:
打印出当前linux 系统支持的所有的错误号:
#include <stdio.h>
#include <errno.h>
#include <string.h>
int main(){
for(int i=;i< ; i++)
printf("errno:%d -> %s\n", i, strerror(i));
return ;
}
当前系统所有错误号:
errno: -> Success
errno: -> Operation not permitted
errno: -> No such file or directory
errno: -> No such process
errno: -> Interrupted system call
errno: -> Input/output error
errno: -> No such device or address
errno: -> Argument list too long
errno: -> Exec format error
errno: -> Bad file descriptor
errno: -> No child processes
errno: -> Resource temporarily unavailable
errno: -> Cannot allocate memory
errno: -> Permission denied
errno: -> Bad address
errno: -> Block device required
errno: -> Device or resource busy
errno: -> File exists
errno: -> Invalid cross-device link
errno: -> No such device
errno: -> Not a directory
errno: -> Is a directory
errno: -> Invalid argument
errno: -> Too many open files in system
errno: -> Too many open files
errno: -> Inappropriate ioctl for device
errno: -> Text file busy
errno: -> File too large
errno: -> No space left on device
errno: -> Illegal seek
errno: -> Read-only file system
errno: -> Too many links
errno: -> Broken pipe
errno: -> Numerical argument out of domain
errno: -> Numerical result out of range
errno: -> Resource deadlock avoided
errno: -> File name too long
errno: -> No locks available
errno: -> Function not implemented
errno: -> Directory not empty
errno: -> Too many levels of symbolic links
errno: -> Unknown error
errno: -> No message of desired type
errno: -> Identifier removed
errno: -> Channel number out of range
errno: -> Level not synchronized
errno: -> Level halted
errno: -> Level reset
errno: -> Link number out of range
errno: -> Protocol driver not attached
errno: -> No CSI structure available
errno: -> Level halted
errno: -> Invalid exchange
errno: -> Invalid request descriptor
errno: -> Exchange full
errno: -> No anode
errno: -> Invalid request code
errno: -> Invalid slot
errno: -> Unknown error
errno: -> Bad font file format
errno: -> Device not a stream
errno: -> No data available
errno: -> Timer expired
errno: -> Out of streams resources
errno: -> Machine is not on the network
errno: -> Package not installed
errno: -> Object is remote
errno: -> Link has been severed
errno: -> Advertise error
errno: -> Srmount error
errno: -> Communication error on send
errno: -> Protocol error
errno: -> Multihop attempted
errno: -> RFS specific error
errno: -> Bad message
errno: -> Value too large for defined data type
errno: -> Name not unique on network
errno: -> File descriptor in bad state
errno: -> Remote address changed
errno: -> Can not access a needed shared library
errno: -> Accessing a corrupted shared library
errno: -> .lib section in a.out corrupted
errno: -> Attempting to link in too many shared libraries
errno: -> Cannot exec a shared library directly
errno: -> Invalid or incomplete multibyte or wide character
errno: -> Interrupted system call should be restarted
errno: -> Streams pipe error
errno: -> Too many users
errno: -> Socket operation on non-socket
errno: -> Destination address required
errno: -> Message too long
errno: -> Protocol wrong type for socket
errno: -> Protocol not available
errno: -> Protocol not supported
errno: -> Socket type not supported
errno: -> Operation not supported
errno: -> Protocol family not supported
errno: -> Address family not supported by protocol
errno: -> Address already in use
errno: -> Cannot assign requested address
errno: -> Network is down
errno: -> Network is unreachable
errno: -> Network dropped connection on reset
errno: -> Software caused connection abort
errno: -> Connection reset by peer
errno: -> No buffer space available
errno: -> Transport endpoint is already connected
errno: -> Transport endpoint is not connected
errno: -> Cannot send after transport endpoint shutdown
errno: -> Too many references: cannot splice
errno: -> Connection timed out
errno: -> Connection refused
errno: -> Host is down
errno: -> No route to host
errno: -> Operation already in progress
errno: -> Operation now in progress
errno: -> Stale file handle
errno: -> Structure needs cleaning
errno: -> Not a XENIX named type file
errno: -> No XENIX semaphores available
errno: -> Is a named type file
errno: -> Remote I/O error
errno: -> Disk quota exceeded
errno: -> No medium found
errno: -> Wrong medium type
errno: -> Operation canceled
errno: -> Required key not available
errno: -> Key has expired
errno: -> Key has been revoked
errno: -> Key was rejected by service
errno: -> Owner died
errno: -> State not recoverable
errno: -> Operation not possible due to RF-kill
errno: -> Memory page has hardware error
errno: -> Unknown error
errno: -> Unknown error
errno: -> Unknown error
errno: -> Unknown error
errno: -> Unknown error
errno: -> Unknown error
保持更新,转载请注明出处。