错误码errno转成对应的错误信息描述

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

int main() {
    int fd, ret;
    enum { LENGTH = 100 };
    char buf[LENGTH];
    fd = open("/tmp/jia", O_RDONLY);
    if(fd == -1) {
        ret = strerror_r(errno, buf, LENGTH);
        if(ret == -1) {
            perror("strerror_r");
            printf("%s
", strerror(errno));
        }
        else { /*success return 1*/
            printf("%s
", buf);
        }
    }
    else {
        ret = close(fd);
        if(ret == -1) {
            perror("close");
            printf("%s
", strerror(errno));
        }
    }

    return 0;
}

 #include<unistd.h>行必须首先出现,因为它定义的与POSIX规范有关的标志可能会影响到其他的头文件。

原文地址:https://www.cnblogs.com/donggongdechen/p/13638889.html