一般当函数错误时,我们会选择打印errno,但实际打印出的errno是一个数字,还需要查手册进一步分析出错原因;我们可以使用两个函数直接打印出错误信息,便于分析
perror,头文件为stdio.h
strerror,头文件为sting.h
函数原型:
void perror(const char *s)
char *strerror(int errnum)
举例当使用fopen时:
FILE *fd = fopen("/usr/cdn","r");
if (fd == NULL)
{
fprintf(stderr,"fopen failed ,errno is %d", errno);
perror("fopen()")
fprintf(stderr,"fopen: %s", strerror(errno));
}
当文件路径错误或文件不存在时,打印错误信息: