Print an error message corresponding to the value of errno
#include <stdio.h>
void perror ( const char *string );
The perror( ) function prints a message to the standard error stream. The output includes first the string referenced by the pointer argument, if any; then a colon and a space, then the error message that corresponds to the current value of the errno variable, ending with a newline character.
Example
#define MSGLEN_MAX 256
FILE *fp;
char msgbuf[MSGLEN_MAX] = "";
if (( fp = fopen( "nonexistentfile", "r" )) == NULL )
{
snprintf( msgbuf, MSGLEN_MAX, "%s, function %s, file %s, line %d",
argv[0], _ _func_ _, _ _FILE_ _, _ _LINE_ _ );
perror( msgbuf );
return errno;
}
Assuming that there is no file available named nonexistentfile, this code results in output like the following on stderr:
./perror, function main, file perror.c, line 18: No such file or directory
See Also
strerror( )
|