Prints an error message.
#include <stdio.h> | Required only for function declarations |
void perror( const char *string );
string | String message to print |
The perror function prints an error message to stderr. The string argument 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 actual 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. The perror function prints the appropriate error message by 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.
To produce accurate results, perror should be called immediately after a library routine returns with an error. Otherwise, the errno value may be overwritten by subsequent calls.
Under DOS, some of the errno values listed in ERRNO.H are not used. These additional errno values are reserved for UNIX use. See “_doserrno, errno, sys_errlist, sys_nerr” for a list of errno values used in DOS and the corresponding error messages. The perror function prints an empty string for any errno value not used under the operating system.
None.
Standards:ANSI, UNIX
16-Bit:DOS, QWIN
32-Bit:DOS32X
/* PERROR.C: This program attempts to open a file named NOSUCHF.ILE.
* Since 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 );
}
}
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