herror
herror()函式用于列印错误信息。
基本介绍
- 中文名:herror
- 包含头档案 :#include <netdb.h>
- 函式定义: void herror( const char *s );
- 保存:为档案 test.c
说明
在使用 gethostbyname() 的时候,你不能用perror() 列印错误信息(因为 errno 没有使用),你应该调用 herror()。
Linux下的 herror(3)函式
内容简介
包含头档案 #include <netdb.h>
函式定义 void herror( const char *s );
函式功能 程式的部分函式运行出错时,先列印出自定义的文字说明字元串 *s ,然后列印出相关错误代码的文字说明。
注意事项 请注意 herror() 与perror() 的区别
与其它相似的函式的区别与联繫
与 perror(3) 的区别:
perror()函式的定义包含于头档案 #include <stdio.h> 一般应与 #include <errno.h> 配合使用
herror()函式的定义包含于头档案 #include <netdb.h> 一般服务于该头档案的函式,如 gethostbyname(3) 、gethostbyaddr(3) 等等。
程式例子(C语言)
/* ********************************************
* 编译:保存为档案 test.c ,在Linux
* 的此档案所在目录下输入命令
* gcc test.c -o test
* (运行)./test
* 程式输出:Can't get IP address: Unknown host
* 错误原因:代码中指代域名的参数 hostname 是一个空字元串 '\0' ;
********************************************* */
#include <netdb.h>
#define NULL (void *)0
int main( int argc, char *argv[ ] )
{
char *hostname = "" ; //申明需要解析的域名地址(不含http://),为了输出错误,这里赋值为空
struct hostent *hent; //存储函式gethostbyname(3)的返回值
if ( (hent = gethostbyname(hostname)) == NULL )
{
herror("Can't get IP address"); // herror(3)在这里被使用
herror("Can't get IP address"); // herror(3)在这里被使用
return -1;
};
};
return 0;
}
/* **************************** END OF TEST.C ***************************************** */