perror, _wperror

列印錯誤訊息。

語法

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

參數

message
要列印的字串訊息。

備註

perror 函式會將錯誤訊息列印至 stderr_wperror 是寬字元版本的 _perrormessage_wperror 引數是寬字元字串。 否則,_wperror_perror 的行為即會相同。

根據預設,此函式的全域狀態會限定于應用程式。 若要變更此行為,請參閱 CRT 中的全域狀態。

泛型文字常式對應

TCHAR.H 常式 _UNICODE_MBCS 未定義 _MBCS 定義 _UNICODE 定義
_tperror perror perror _wperror

message 會先列印,後面接著冒號,再接著上次產生錯誤之程式庫呼叫的系統錯誤訊息,最後接著新行字元。 如果 message 是 null 指標或 Null 字串的指標,perror 只會列印系統錯誤訊息。

錯誤號碼會儲存在 變數 errno 中(定義于 ERRNO 中。H). 系統會透過 變數 _sys_errlist 存取系統錯誤訊息,這是依錯誤號碼排序的訊息陣列。 perror 會使用 errno 值作為 _sys_errlist 的索引,來列印適當的錯誤訊息。 變數 _sys_nerr 的值會定義為數組中 _sys_errlist 專案數目上限。

如需精確的結果,請在程式庫常式傳回錯誤之後立即呼叫 perror 。 否則,後續呼叫可能會覆寫 errno 值。

在 Windows 作業系統中,不會用到 ERRNO.H 中所列的一些 errno 值。 這些值會保留供 UNIX 作業系統使用。 如需 Windows 作業系統所使用的值清單,請參閱 errno_doserrno_sys_errlist_sys_nerrerrno perror 會針對這些平台未使用的任何 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 );
   }
}
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

另請參閱

進程和環境控制
clearerr
ferror
strerror, _strerror, _wcserror, __wcserror