侧边栏壁纸
博主头像
noerror

虚灵不寐,众理具而万事出。

  • 累计撰写 239 篇文章
  • 累计创建 9 个标签
  • 累计收到 2 条评论
标签搜索

目 录CONTENT

文章目录

err函数用法详解

noerror
2022-11-08 / 0 评论 / 0 点赞 / 173 阅读 / 431 字 / 正在检测是否收录...

err函数用法详解

err函数简介

  • 头文件包含
#include <err.h>
#include <stdarg.h>
  • 函数定义
void err(int  eval , const char * fmt , ...);
void errx(int  eval , const char * fmt , ...);
void warn(const char * fmt , ...);
void warnx(const char * fmt , ...);
void verr(int  eval , const char * fmt , va_list  args );
void verrx(int  eval , const char * fmt , va_list  args );
void vwarn(const char * fmt , va_list  args );
void vwarnx(const char * fmt , va_list  args );

err函数常见使用错误

  • 编译错误
    warning: implicit declaration of function ‘err’ [-Wimplicit-function-declaration]
    解决办法:包含头文件
#include <err.h>
#include <stdarg.h>

err函数详细描述

err ()和warn ()系列函数在标准错误输出上显示格式化的错误消息。在所有情况下,都会输出程序名的最后一个组成部分,一个冒号字符和一个空格。如果fmt参数不为空,则输出printf (3)-like格式的错误消息。输出以换行符结束。
err ()、verr ()、warn ()和vwarn ()函数根据全局变量errno 附加从strerror (3)获得的错误消息,除非fmt参数为NULL。
errx ()和warnx ()函数不附加错误消息。
err () verr () errx ()和verrx ()函数不返回,但以参数eval 的值退出

err函数使用举例

显示当前errno信息字符串并退出:

p = malloc(size);
if (p == NULL)
   err(EXIT_FAILURE, NULL);
fd = open(file_name, O_RDONLY, 0);
if (fd == \-1)
   err(EXIT_FAILURE, "%s", file_name);

显示错误消息并退出:

if (tm.tm_hour < START_TIME)
   errx(EXIT_FAILURE, "too early, wait until %s",
           start_time_string);

错误警告:

fd = open(raw_device, O_RDONLY, 0);
if (fd == \-1)
   warnx("%s: %s: trying the block device",
           raw_device, strerror(errno));
fd = open(block_device, O_RDONLY, 0);
if (fd == \-1)
   err(EXIT_FAILURE, "%s", block_device);
0

评论区