perror、_wperrorperror, _wperror

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

语法Syntax

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

参数Parameters

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

备注Remarks

Perror函数将错误消息打印到stderrThe perror function prints an error message to stderr. _wperror_perror的宽字符版本; _wperrormessage参数是宽字符字符串。_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. 如果message为 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_errlist的索引来打印相应的错误消息。perror 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.

为获得准确的结果,请在库例程返回错误后立即调用perrorFor 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. 有关 Windows 操作系统使用的errno值的列表,请参阅_doserrno、errno、_sys_errlist 和 _sys_nerrSee _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.

要求Requirements

例程所返回的值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.

Libraries

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

示例Example

// 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 );
   }
}
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
clearerrclearerr
ferrorferror
strerror、_strerror、_wcserror、__wcserrorstrerror, _strerror, _wcserror, __wcserror