perror 函数

perror编辑

perror( ) 用来将上一个函数发生错误的原因输出到标准设备(stderr)。参数 s 所指的字符串会先打印出,后面再加上错误原因字符串。此错误原因依照全局变量errno 的值来决定要输出的字符串。
在库函数中有个errno变量,每个errno值对应着以字符串表示的错误类型。当你调用"某些"函数出错时,该函数已经重新设置了errno的值。perror函数只是将你输入的一些信息和现在的errno所对应的错误一起输出。
中文名
perror
头文件
#include<stdio.h>
注    意
不可以丢了#include<stdio.h>
定义函数
void perror

1表头文件编辑

头文件

#include<stdio.h>
#include<stdlib.h>
注意
不可以丢了#include<stdio.h>这个头文件,perror是包含在这个文件里的

2完善版编辑

定义函数

void perror(const char *s); perror ("open_port");
范例
1
2
3
4
5
6
7
8
9
10
11
#include<stdio.h>
intmain(void)
{
FILE*fp;
fp=fopen("/root/noexitfile","r+");
if(NULL==fp)
{
perror("/root/noexitfile");
}
return0;
}

运行结果

[root@localhost io]# gcc perror.c
[root@localhost io]# ./a.out
/root/noexitfile: No such file or directory
 
转自 : http://baike.baidu.com/link?url=S8HvJ70Qt329eeeJkJWbQUH5QR3L0iO1bHBKyL7nJ6TAd_wqrFx2d3WgIfSXeZTvurvqEUkp1_XEfkV8jCAn5_
原文地址:https://www.cnblogs.com/IceSword-syy/p/4261877.html