perror()函数
函数原型:void perror ( const char * str );
perror(str) 用来将上一个函数发生错误的原因输出到标准设备(stderr)。参数 str 所指的字符串会先打印出,后面再加上错误原因字符串。此错误原因依照全局变量errno的值来决定要输出的字符串。
在库函数中有个errno变量,每个errno值对应着以字符串表示的错误类型。当你调用"某些"函数出错时,该函数已经重新设置了errno的值。perror函数只是将你输入的一些信息和现在的errno所对应的错误一起输出。
示例代码:
#include<stdio.h>
int main(void)
{
FILE *fp;
fp=fopen("/root/noexitfile","r+");
if(NULL==fp)
{
perror("/root/noexitfile");
}
return 0;
}
运行结果:
/root/noexitfile: No such file or directory
参考自百度百科:
https://baike.baidu.com/item/perror/6828989