函式
<cstring>

strerror

char * strerror ( int errnum );
獲取指向錯誤資訊字串的指標
解釋 errnum 的值,生成一個帶有訊息的字串,該訊息描述錯誤條件,就像庫函式將它設定為 errno 一樣。

返回的指標指向一個靜態分配的字串,程式不應修改該字串。對此函式的進一步呼叫可能會覆蓋其內容(不要求特定的庫實現避免資料競爭)。

strerror生成的錯誤字串可能因每個系統和庫實現而異。

引數

errnum
錯誤碼。

返回值

一個指向描述錯誤 errnum 的錯誤字串的指標。

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
/* strerror example : error list */
#include <stdio.h>
#include <string.h>
#include <errno.h>

int main ()
{
  FILE * pFile;
  pFile = fopen ("unexist.ent","r");
  if (pFile == NULL)
    printf ("Error opening file unexist.ent: %s\n",strerror(errno));
  return 0;
}

可能的輸出

Error opening file unexist.ent: No such file or directory


另見