errno:在系统出现错误时内核把错误写入到变量errno中。在系统调用返回失败时,必须紧接着引用errno变量,避免程序中的下一条系统调用时出错覆盖errno的值。
perror()和strerror():用来打印错误提示信息。
perror:将错误信息出处到终端。
int main()
{
FILE * pFile;
pFile = fopen("unexist.ent", "rb");
if (pFile == NULL)
perror("The following error occurred");
else
fclose(pFile);
return 0;
}
运行结果:
strerror:将错误信息输出到指定的缓冲区。
int main()
{
FILE * pFile;
pFile = fopen("unexist.ent", "rb");
if (pFile == NULL)
printf("%s", strerror(errno)); //打印错误
else
fclose(pFile);
return 0;
}
运行结果: