strerror, _strerror
Get a system error message (strerror) or prints a user-supplied error message (_strerror).
char*strerror(interrnum**);**
char*_strerror(constchar*strErrMsg);
Routine | Required Header | Compatibility |
strerror | <string.h> | ANSI, Win 95, Win NT |
_strerror | <string.h> | Win 95, Win NT |
For additional compatibility information, see Compatibility in the Introduction.
Libraries
LIBC.LIB | Single thread static library, retail version |
LIBCMT.LIB | Multithread static library, retail version |
MSVCRT.LIB | Import library for MSVCRT.DLL, retail version |
Return Value
strerror and _strerror return a pointer to the error-message string. Subsequent calls to strerror or _strerror can overwrite the string.
Parameters
errnum
Error number
strErrMsg
User-supplied message
Remarks
The strerror function maps errnum to an error-message string, returning a pointer to the string. Neither strerror nor _strerror actually prints the message: For that, you need 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 containing 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 containing (in order) your string message, a colon, a space, the system error message for the last library call producing an error, and a newline character. Your string message can be, at most, 94 bytes long.
The actual error number for _strerror is stored in the variable errno. The system error messages are accessed through the variable _sys_errlist, which is an array of messages ordered by error number. _strerror accesses the appropriate error message by using the errno value as an index to the variable _sys_errlist. The value of the variable _sys_nerr is defined as the maximum number of elements in the _sys_errlist array. 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.
_strerror is not part of the ANSI definition but is instead a Microsoft extension to it. Do not use it where portability is desired; for ANSI compatibility, use strerror instead.
Example
/* PERROR.C: 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>
void main( void )
{
int fh;
if( (fh = _open( "NOSUCHF.ILE", _O_RDONLY )) == -1 )
{
/* Three ways to create error message: */
perror( "perror says open failed" );
printf( "strerror says open failed: %s\n", strerror( errno ) );
printf( _strerror( "_strerror says open failed" ) );
}
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