函式名
strerror, _strerror, _wcserror, __wcserror
函式作用
Get a system error message (strerror, _wcserror) or prints a user-supplied error message (_strerror, __wcserror).
獲取系統錯誤信息或列印用戶程式錯誤信息。
函式原型
參數:
errnum:錯誤標號,通常用errno(標準錯誤號,定義在errno.h中)
Error number.
strErrMsg
User-supplied message.
返回:
指向錯誤信息的指針(即:錯誤的描述字元串)。
舉例
例一:
輸出:
error=2
Mesg:No such file or direcory
例二:
// crt_perror.c
// compile with: /W1
/* 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
#include
#include
#include
#include
#include
#include
#include
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