perror, errno, strerror 三个函数的说明

来源:互联网 发布:淘宝代购港版手机 编辑:程序博客网 时间:2024/05/21 11:13
#include <stdio.h> // void perror(const char *msg);
#include <errno.h> //errno
#include <string.h> // char *strerror(int errnum);

errno 是错误代码,在 errno.h头文件中;
perror是错误输出函数,输出格式为:msg:errno对应的错误信息(加上一个换行符);

strerror是通过参数 errnum (就是errno),返回对应的错误信息。

示例:

... ...if( (fp = fopen(argv[1], "r")) == NULL){perror("perror");  errno = 12;  printf("strerror: %s\n", strerror(errno)); //转换错误码为对应的错误信息exit(1);}... ...