December 2005
Beginner to intermediate
618 pages
20h 19m
English
perror
Print an error message corresponding to the value of errno
#include <stdio.h> voidperror( 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.
#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
Read now
Unlock full access