perror _wperror

列印錯誤訊息。

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

參數

  • string
    若要列印的字串訊息。

備註

perror函式會列印錯誤訊息stderr_wperror寬字元版本的 _perror。 string引數為**_wperror是寬字元字串。_wperror**與 _perror 其他方式完全相同。

泛用文字常式對應

TCHAR。H 常式

_UNICODE & 未定義的 _MBCS

定義的 _MBCS

定義 _unicode 之後

_tperror

perror

perror

_wperror

string會先列印後, 接一個冒號,[次要鍵] 的最後一個程式庫呼叫所產生的錯誤,系統錯誤訊息最後新行字元。如果string時發現 null 指標或指標為 null 字串, perror列印只系統錯誤訊息。

儲存在變數中的錯誤代碼 errno (如 ERRNO 所述。H) 中。系統錯誤訊息透過變數存取 _sys_errlist,也就是郵件已排序的錯誤編號的陣列。perror列印適當的錯誤訊息使用errno值當作此引數才**_sys_errlist**。變數的值 _sys_nerr 中的項目數目上限指**_sys_errlist**陣列。

精確的結果,呼叫perror程式庫常式因發生錯誤的傳回立即之後。否則,後續的呼叫可以覆寫errno的值。

在 Windows 作業系統,有些errno ERRNO 中列出的值。H 一般不會使用。這些值被保留供使用 UNIX 作業系統。請參閱 _doserrno、 errno、 _sys_errlist,以及 _sys_nerr 如需清單errno與 Windows 作業系統所使用的值。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 );
   }
}

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