strerror、_strerror、_wcserror、__wcserrorstrerror, _strerror, _wcserror, __wcserror

获取系统错误消息字符串(strerror_wcserror),或设置用户提供的错误消息字符串( _strerror__wcserror)的格式。Gets a system error message string (strerror, _wcserror) or formats a user-supplied error message string (_strerror, __wcserror). 这些函数的更安全版本已经发布,请参阅 strerror_s、_strerror_s、_wcserror_s、__wcserror_sMore secure versions of these functions are available; see strerror_s, _strerror_s, _wcserror_s, __wcserror_s.


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


错误号。Error number.

用户提供的消息。User-supplied message.

返回值Return Value

所有这些函数都将返回指向错误消息字符串的指针。All of these functions return a pointer to the error-message string. 后续调用可覆盖该字符串。Subsequent calls can overwrite the string.


Strerror函数将errnum映射到错误消息字符串,并返回指向该字符串的指针。The strerror function maps errnum to an error-message string and returns a pointer to the string. Strerror_strerror都不会实际打印消息:为此,必须调用输出函数,例如fprintfNeither 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) );

如果strErrMsg传递为NULL,则 _strerror将返回一个指向字符串的指针,该字符串包含产生错误的最后一个库调用的系统错误消息。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. 错误消息字符串以换行符 ('\n') 结尾。The error-message string is terminated by the newline character ('\n'). 如果strErrMsg不等于NULL,则 _strerror将返回一个指向字符串的指针,该字符串包含(按顺序)你的字符串消息、一个冒号、一个空格、产生错误的最后一个库调用的系统错误消息,以及一个换行符字符.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. 你的字符串消息长度最多可达 94 个字符。Your string message can be, at most, 94 characters long.

_Strerror的实际错误号存储在变量errno中。The actual error number for _strerror is stored in the variable errno. 若要生成准确的结果,请在库例程返回错误后立即调用 _strerrorTo produce accurate results, call _strerror immediately after a library routine returns with an error. 否则,对strerror_strerror的后续调用可能会覆盖errno值。Otherwise, subsequent calls to strerror or _strerror can overwrite the errno value.

_wcserror__wcserror分别是strerror_strerror的宽字符版本。_wcserror and __wcserror are wide-character versions of strerror and _strerror, respectively.

_strerror_wcserror__wcserror不是 ANSI 定义的一部分;它们是 Microsoft 扩展,我们建议你不要在需要可移植代码的位置使用它们。_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. 对于 ANSI 兼容性,请改用strerrorFor ANSI compatibility, use strerror instead.

若要获取错误字符串,我们建议使用strerror_wcserror ,而不是已弃用的宏_sys_errlist_sys_nerr以及弃用的内部函数 __sys_errlist__sys_nerrTo 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 例程TCHAR.H routine 未定义 _UNICODE 和 _MBCS_UNICODE & _MBCS not defined 已定义 _MBCS_MBCS defined 已定义 _UNICODE_UNICODE defined
_tcserror_tcserror strerrorstrerror strerrorstrerror _wcserror_wcserror


例程所返回的值Routine 必需的标头Required header
strerrorstrerror <string.h><string.h>
_strerror_strerror <string.h><string.h>
_wcserror__wcserror_wcserror, __wcserror <string.h><string.h>

有关其他兼容性信息,请参阅 兼容性For additional compatibility information, see Compatibility.


请参阅 perror 示例。See the example for perror.

请参阅See also

字符串操作String Manipulation
perror、_wperrorperror, _wperror