perror, _wperror
Print an error message.
voidperror(constchar *string**);**
void_wperror(constwchar_t *string**);**
Routine | Required Header | Compatibility |
perror | <stdio.h> or <stdlib.h> | ANSI, Win 95, Win NT |
_wperror | <stdio.h> or <wchar.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
None
Parameter
string
String message to print
Remarks
The perror function prints an error message to stderr. _wperror is a wide-character version of _perror; the string argument to _wperror is a wide-character string. _wperror and _perror behave identically otherwise.
Generic-Text Routine Mappings
TCHAR.H Routine | _UNICODE & _MBCS Not Defined | _MBCS Defined | _UNICODE Defined |
_tperror | perror | perror | _wperror |
string is printed first, followed by a colon, then by the system error message for the last library call that produced the error, and finally by a newline character. If string is a null pointer or a pointer to a null string, perror prints only the system error message.
The error number is stored in the variable errno (defined in ERRNO.H). The system error messages are accessed through the variable _sys_errlist, which is an array of messages ordered by error number. perror prints the appropriate error message using the errno value as an index to _sys_errlist. The value of the variable _sys_nerr is defined as the maximum number of elements in the _sys_errlist array.
For accurate results, call perror immediately after a library routine returns with an error. Otherwise, subsequent calls can overwrite the errno value.
In Windows NT and Windows 95, some errno values listed in ERRNO.H are unused. These values are reserved for use by the UNIX operating system. See _doserrno, errno, _sys_errlist, and _sys_nerr for a listing of errno values used by Windows NT and Windows 95. perror prints an empty string for any errno value not used by these platforms.
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