共用方式為


strerror, _strerror, _wcserror, __wcserror

 

The latest version of this topic can be found at strerror, _strerror, _wcserror, __wcserror.

Gets a system error message string (strerror, _wcserror) or formats a user-supplied error message string (_strerror, __wcserror). More secure versions of these functions are available; see strerror_s, _strerror_s, _wcserror_s, __wcserror_s.

Syntax

char *strerror(  
   int errnum   
);  
char *_strerror(  
   const char *strErrMsg   
);  
wchar_t * _wcserror(  
   int errnum   
);  
wchar_t * __wcserror(  
   const wchar_t *strErrMsg   
);  

Parameters

errnum
Error number.

strErrMsg
User-supplied message.

Return Value

All of these functions return a pointer to the error-message string. Subsequent calls can overwrite the string.

Remarks

The strerror function maps errnum to an error-message string and returns a pointer to the string. Neither strerror nor _strerror actually prints the message: For that, you have to call an output function such as fprintf:

if (( _access( "datafile",2 )) == -1 )  
   fprintf( stderr, _strerror(NULL) );  

If strErrMsg is passed as NULL, _strerror returns a pointer to a string that contains the system error message for the last library call that produced an error. The error-message string is terminated by the newline character ('\n'). If strErrMsg is not equal to NULL, then _strerror returns a pointer to a string that contains (in order) your string message, a colon, a space, the system error message for the last library call that produces an error, and a newline character. Your string message can be, at most, 94 characters long.

The actual error number for _strerror is stored in the variable errno. To produce accurate results, call _strerror immediately after a library routine returns with an error. Otherwise, subsequent calls to strerror or _strerror can overwrite the errno value.

_wcserror and __wcserror are wide-character versions of strerror and _strerror, respectively.

_strerror, _wcserror, and __wcserror are not part of the ANSI definition; they are Microsoft extensions and we recommend that you do not use them where you want portable code. For ANSI compatibility, use strerror instead.

To get error strings, we recommend strerror or _wcserror instead of the deprecated macros _sys_errlist and _sys_nerr and the deprecated internal functions __sys_errlist and __sys_nerr.

Generic-Text Routine Mappings

TCHAR.H routine _UNICODE & _MBCS not defined _MBCS defined _UNICODE defined
_tcserror strerror strerror _wcserror

Requirements

Routine Required header
strerror <string.h>
_strerror <string.h>
_wcserror, __wcserror <string.h>

For additional compatibility information, see Compatibility.

Example

See the example for perror.

.NET Framework Equivalent

System::Exception::Message

See Also

String Manipulation
clearerr
ferror
perror, _wperror