栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

linux C语言perror()函数(将错误消息写入标准错误)(把一个描述性错误消息输出到标准错误 stderr。首先输出自定义字符串 str,后跟一个冒号,然后是一个空格)

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

linux C语言perror()函数(将错误消息写入标准错误)(把一个描述性错误消息输出到标准错误 stderr。首先输出自定义字符串 str,后跟一个冒号,然后是一个空格)

文章目录
    • man 3 perror
    • man 3posix perror

man 3 perror
PERROR(3)                                                            Linux Programmer's Manual                                                           PERROR(3)

NAME
       perror - print a system error message	//打印系统错误信息

SYNOPSIS
       #include 

       void perror(const char *s);

       #include 

       const char * const sys_errlist[];
       int sys_nerr;
       int errno;       

   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

       sys_errlist, sys_nerr: _BSD_SOURCE

DESCRIPTION
       The perror() function produces a message on standard error describing the last error encountered during a call to a system or library function.
       //perror() 函数在标准错误上生成一条消息,描述在调用系统或库函数期间遇到的最后一个错误。

       First (if s is not NULL and *s is not a null byte ('')), the argument string s is printed, followed by a colon and a blank.  Then an error message corre‐
       sponding to the current value of errno and a new-line.
       //首先(如果 s 不是 NULL 并且 *s 不是空字节 ('')),则打印参数字符串 s,后跟一个冒号和一个空格。 
       //然后是与 errno 的当前值相对应的错误消息和换行符。

       To be of most use, the argument string should include the name of the function that incurred the error.
       //最常用的是,参数字符串应该包含导致错误的函数的名称。

       The global error list sys_errlist[], which can be indexed by errno, can be used to obtain the error message without the newline.  The largest message  num‐
       ber  provided in the table is sys_nerr-1.  Be careful when directly accessing this list, because new error values may not have been added to sys_errlist[].
       The use of sys_errlist[] is nowadays deprecated.
       //可以通过errno索引的全局错误列表sys_errlist[],可以用来获取不带换行符的错误信息。 
       //表中提供的最大消息编号是 sys_nerr-1。 
       //直接访问此列表时要小心,因为新的错误值可能尚未添加到 sys_errlist[]。
        //现在不推荐使用 sys_errlist[] 。

       When a system call fails, it usually returns -1 and sets the variable errno to a value  describing  what  went  wrong.   (These  values  can  be  found  in
       .)   Many  library functions do likewise.  The function perror() serves to translate this error code into human-readable form.  Note that errno is
       undefined after a successful sysme call or library function call: this call may well change this variable, even though it succeeds, for example because  it
       internally  used  some  other  library function that failed.  Thus, if a failing call is not immediately followed by a call to perror(), the value of errno
       should be saved.
       //当系统调用失败时,它通常会返回 -1 并将变量 errno 设置为描述错误原因的值。 
       //(这些值可以在  中找到。)许多库函数也是如此。 
       //函数 perror() 用于将此错误代码转换为人类可读的形式。 
       //请注意,在成功的系统调用或库函数调用之后,errno 是未定义的:此调用很可能会更改此变量,即使它成功,例如因为它在内部使用了其他失败的库函数。 
       //因此,如果调用失败后没有立即调用 perror(),则应保存 errno 的值。

ATTRIBUTES
       For an explanation of the terms used in this section, see attributes(7).

       ┌──────────┬───────────────┬─────────────────────┐
       │Interface │ Attribute     │ Value               │
       ├──────────┼───────────────┼─────────────────────┤
       │perror()  │ Thread safety │ MT-Safe race:stderr │
       └──────────┴───────────────┴─────────────────────┘

CONFORMING TO
       perror(), errno: POSIX.1-2001, POSIX.1-2008, C89, C99, 4.3BSD.

       The externals sys_nerr and sys_errlist derive from BSD, but are not specified in POSIX.1.

NOTES
       The externals sys_nerr and sys_errlist are defined by glibc, but in .

SEE ALSO
       err(3), errno(3), error(3), strerror(3)

COLOPHON
       This page is part of release 4.04 of the Linux man-pages project.  A description of the project, information about reporting bugs, and the  latest  version
       of this page, can be found at http://www.kernel.org/doc/man-pages/.

                                                                            2015-07-23                                                                   PERROR(3)
 Manual page perror(3) line 22/63 (END) (press h for help or q to quit)

man 3posix perror
PERROR(3POSIX)                                                       POSIX Programmer's Manual                                                      PERROR(3POSIX)

PROLOG
       This  manual  page is part of the POSIX Programmer's Manual.  The Linux implementation of this interface may differ (consult the corresponding Linux manual
       page for details of Linux behavior), or the interface may not be implemented on Linux.
       //本手册页是 POSIX 程序员手册的一部分。 
       //此接口的 Linux 实现可能不同(有关 Linux 行为的详细信息,请参阅相应的 Linux 手册页),或者该接口可能未在 Linux 上实现。

NAME
       perror — write error messages to standard error	//将错误消息写入标准错误

SYNOPSIS
       #include 

       void perror(const char *s);

DESCRIPTION
       The functionality described on this reference page is aligned with the ISO C standard. Any conflict between the requirements described here and  the  ISO C
       standard is unintentional. This volume of POSIX.1‐2008 defers to the ISO C standard.
		//本参考页上描述的功能符合 ISO C 标准。 
		//此处描述的要求与 ISO C 标准之间的任何冲突都是无意的。 
		//本卷 POSIX.1-2008 遵循 ISO C 标准。

       The  perror()  function  shall  map the error number accessed through the symbol errno to a language-dependent error message, which shall be written to the
       standard error stream as follows:
       //perror() 函数应将通过符号 errno 访问的错误号映射到与语言相关的错误消息,
       //该错误消息应写入标准错误流,如下所示:

        *  First (if s is not a null pointer and the character pointed to by s is not the null byte), the string pointed to by s  followed  by  a    and  a
           .
           //首先(如果 s 不是空指针并且 s 指向的字符不是空字节),
           //s 指向的字符串后跟  和 。(接冒号和空格(": "))

        *  Then an error message string followed by a .
        	//然后是一个错误消息字符串,后跟一个 <换行符>。

       The contents of the error message strings shall be the same as those returned by strerror() with argument errno.
       //错误消息字符串的内容应与带参数 errno 的 strerror() 返回的内容相同。

       The  perror()  function  shall  mark  for update the last data modification and last file status change timestamps of the file associated with the standard
       error stream at some time between its successful completion and exit(), abort(), or the completion of fflush() or fclose() on stderr.
       //perror() 函数应在其成功完成和 exit()、abort() 或 fflush( ) 或 stderr 上的 fclose()。

       The perror() function shall not change the orientation of the standard error stream.
       //perror() 函数不应改变标准错误流的方向。

       On error, perror() shall set the error indicator for the stream to which stderr points, and shall set errno to indicate the error.
       //发生错误时,perror() 应为 stderr 指向的流设置错误指示符,并应设置 errno 以指示错误。

       Since no value is returned, an application wishing to check for error situations should  call  clearerr(stderr)  before  calling  perror(),  then  if  fer‐
       ror(stderr) returns non-zero, the value of errno indicates which error occurred.
       //由于没有返回值,因此希望检查错误情况的应用程序应在调用 perror() 之前调用 clearerr(stderr),然后如果 fer-ror(stderr) 返回非零,则 errno 的值指示发生了哪个错误。

RETURN VALUE
       The perror() function shall not return a value.
       //perror() 函数不应返回值。

ERRORS
       Refer to fputc().

       The following sections are informative.

EXAMPLES
   Printing an Error Message for a Function
       The  following example replaces bufptr with a buffer that is the necessary size. If an error occurs, the perror() function prints a message and the program
       exits.
       //以下示例将 bufptr 替换为必要大小的缓冲区。 
       //如果发生错误,perror() 函数会打印一条消息并退出程序。

           #include 
           #include 
           ...
           char *bufptr;
           size_t szbuf;
           ...
           if ((bufptr = malloc(szbuf)) == NULL) {
               perror("malloc"); exit(2);
           }
           ...

APPLICATION USAGE
       Application writers may prefer to use alternative interfaces instead of perror(), such as strerror_r() in combination with fprintf().
       //应用程序编写者可能更喜欢使用替代接口而不是 perror(),例如 strerror_r() 与 fprintf() 的组合。

RATIONALE
       None.

FUTURE DIRECTIONS
       None.

SEE ALSO
       fprintf(), fputc(), psiginfo(), strerror()

       The Base Definitions volume of POSIX.1‐2008, 

COPYRIGHT
       Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology  --  Portable
       Operating  System  Interface  (POSIX),  The Open Group Base Specifications Issue 7, Copyright (C) 2013 by the Institute of Electrical and Electronics Engi‐
       neers, Inc and The Open Group.  (This is POSI

参考文章1:C 库函数 - perror()

参考文章2:perror()函数的使用

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/964964.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号