← 返回首页
Linux高级程序设计(十八)
发表时间:2021-11-08 21:10:28
输出错误信息

函数调用后输出错误信息有以下两种方式。

1.errno

#include <errno.h>

//errno是全局变量,通过errno获取错误码。

实例:

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>

int main(int argc,char const *argv[]){
    int fd;
    //打开的文件test.txt不存在。
    fd = open("test.txt", O_RDONLY);
    if(fd==-1){
        printf("errno=%d\n", errno);
        return -1;
    }
    return 0;
}

运行结果:

errno=2

每一个错误码都对应一个错误消息。通过查看 /usr/include/asm-generic/errno-base.h 可获取详情。

[root@iz2zefozq9h39txdb8s7npz asm-generic]# cat /usr/include/asm-generic/errno-base.h
#ifndef _ASM_GENERIC_ERRNO_BASE_H
#define _ASM_GENERIC_ERRNO_BASE_H

#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 */

#endif

2.perror

#include <stdio.h>
void perror(const char * s);

实例:

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>

int main(int argc,char const *argv[]){
    int fd;
    //打开的文件test.txt不存在。
    fd = open("test.txt", O_RDONLY);
    if(fd==-1){
        perror("fail open file:");
        return -1;
    }
    return 0;
}

运行结果:

fail open file:: No such file or directory