perror, _wperror

打印一条错误消息。

void perror(
   const char *string 
);
void _wperror(
   const wchar_t *string 
);

参数

  • string
    打印的字符串消息。

备注

perror 功能打印一条错误消息。 stderr_wperror_perror的宽字符版本;为 _wperror 的 string 参数是宽字符字符串。 _wperror_perror 否则具有相同的行为。

一般文本例程映射

TCHAR.H 实例

未定义的 _UNICODE _MBCS

定义的 _MBCS

定义的 _UNICODE

_tperror

perror

perror

_wperror

string 冒号首先打印,按照,然后按最后一个库的系统错误信息调用该生成的错误,最后是由换行符。 如果 string 是 null 指针或指针为空字符串, perror 打印只有系统错误信息。

错误号在变量 errno 存储区 (定义在 ERRNO.H)。 系统错误信息通过变量的 _sys_errlist中,是消息在失误经过排序的数字。 perror 打印相应的错误消息使用 errno 值作为索引来 _sys_errlist。 变量 _sys_nerr 的值定义,元素的最大数在 _sys_errlist 数组。

对于准确的结果,因此,在库实例返回一个错误后,调用 perror 。 否则,对的后续调用都可以复盖 errno 值。

在 windows 操作系统中, ERRNO.H 列出的那些 errno 值不使用。 这些值是用于 UNIX 操作系统使用。 list windows 操作系统使用的 errno 值参见 _doserrno、 errno、 _sys_errlist 和 _sys_nerrperror 打印这些平台不使用的所有 errno 值的空字符串。

要求

实例

必需的头

perror

stdio.h 或 stdlib.h

_wperror

stdio.h 或 wchar.h

有关其他的兼容性信息,请参见中介绍的 兼容性

C 运行库的所有版本。

示例

// crt_perror.c
// compile with: /W3
/* This program attempts to open a file named
 * NOSUCHF.ILE. Because this file probably doesn't exist,
 * an error message is displayed. The same message is
 * created using perror, strerror, and _strerror.
 */

#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <io.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <share.h>

int main( void )
{
   int  fh;

   if( _sopen_s( &fh, "NOSUCHF.ILE", _O_RDONLY, _SH_DENYNO, 0 ) != 0 )
   {
      /* Three ways to create error message: */
      perror( "perror says open failed" );
      printf( "strerror says open failed: %s\n",
         strerror( errno ) ); // C4996
      printf( _strerror( "_strerror says open failed" ) ); // C4996
      // Note: strerror and _strerror are deprecated; consider
      // using strerror_s and _strerror_s instead.
   }
   else
   {
      printf( "open succeeded on input file\n" );
      _close( fh );
   }
}

Output

perror says open failed: No such file or directory
strerror says open failed: No such file or directory
_strerror says open failed: No such file or directory

.NET Framework 等效项

不适用。若要调用标准 C 函数,请使用 PInvoke。有关更多信息,请参见 平台调用示例

请参见

参考

处理和环境控件

clearerr

ferror

strerror, _strerror, _wcserror, __wcserror