perror、_wperrorperror, _wperror

打印错误消息。Print an error message.


void perror(
   const char *message
void _wperror(
   const wchar_t *message


消息要打印的字符串消息。message String message to print.


Perror函数将打印错误消息到stderrThe perror function prints an error message to stderr. _wperror是宽字符版本的 _perror;消息参数 _wperror是宽字符字符串。_wperror is a wide-character version of _perror; the message argument to _wperror is a wide-character string. _wperror_perror否则具有相同行为。_wperror and _perror behave identically otherwise.

一般文本例程映射Generic-Text Routine Mappings

TCHAR.H 例程TCHAR.H routine 未定义 _UNICODE 和 _MBCS_UNICODE & _MBCS not defined 已定义 _MBCS_MBCS defined 已定义 _UNICODE_UNICODE defined
_tperror_tperror perrorperror perrorperror _wperror_wperror

消息打印第一次后, 接一个冒号,则通过生成错误,最后一次库调用的系统错误消息,最后通过换行字符。message is printed first, followed by a colon, then by the system error message for the last library call that produced the error, and finally by a newline character. 如果消息是 null 指针或指向空字符串, perror打印系统的错误消息。If message is a null pointer or a pointer to a null string, perror prints only the system error message.

错误数字存储在变量 errno 中(在 ERRNO.H 中定义)。The error number is stored in the variable errno (defined in ERRNO.H). 通过变量 _sys_errlist 访问系统错误消息,该变量是按错误编号排序的消息数组。The system error messages are accessed through the variable _sys_errlist, which is an array of messages ordered by error number. perror打印相应的错误消息使用errno的索引值 _sys_errlistperror prints the appropriate error message using the errno value as an index to _sys_errlist. 变量的值_sys_nerr定义中的元素的最大数目为 _sys_errlist数组。The value of the variable _sys_nerr is defined as the maximum number of elements in the _sys_errlist array.

获得准确的结果调用perror库例程返回错误后立即。For accurate results, call perror immediately after a library routine returns with an error. 否则,后续调用可以覆盖errno值。Otherwise, subsequent calls can overwrite the errno value.

在 Windows 操作系统,某些errno ERRNO 中列出的值。H 是未使用。In the Windows operating system, some errno values listed in ERRNO.H are unused. 这些值将保留以供 UNIX 操作系统使用。These values are reserved for use by the UNIX operating system. 请参阅_doserrno,errno,_sys_errlist 和 _sys_nerr有关的列表errno使用的 Windows 操作系统的系统值。See _doserrno, errno, _sys_errlist, and _sys_nerr for a listing of errno values used by the Windows operating system. perror任何打印一个空字符串errno不使用这些平台的值。perror prints an empty string for any errno value not used by these platforms.


例程Routine 必需的标头Required header
perrorperror <stdio.h> 或 <stdlib.h><stdio.h> or <stdlib.h>
_wperror_wperror <stdio.h> 或 <wchar.h><stdio.h> or <wchar.h>

有关其他兼容性信息,请参阅 兼容性For additional compatibility information, see Compatibility.


C 运行时库的所有版本。All versions of the C run-time libraries.


// 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.
      printf( "open succeeded on input file\n" );
      _close( fh );
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

请参阅See also

进程和环境控制Process and Environment Control
strerror、_strerror、_wcserror、__wcserrorstrerror, _strerror, _wcserror, __wcserror