function
perror
<cstdio>
void perror ( const char * str );
Print error message
Interprets the value of the global variable
errno into a string and prints that string to
stderr (standard error output stream, usually the screen), optionaly preceding it with the custom message specified in
str.
errno is an integral variable whose value describes the last error produced by a call to a library function. The error strings produced by
perror depend on the developing platform and compiler.
If the parameter
str is not a null pointer,
str is printed followed by a colon (:) and a space. Then, whether
str was a null pointer or not, the generated error description is printed followed by a newline character (
'\n').
perror should be called right after the error was produced, otherwise it can be overwritten in calls to other functions.
Parameters.
- str
- C string containing a custom message to be printed before the error message itself.
If it is a null pointer, no preceding custom message is printed, but the error message is printed anyway.
By convention, the name of the application itself is generally used as parameter.
Return Value
none
Example
1 2 3 4 5 6 7 8 9 10 11 12 13
|
/* perror example */
#include <stdio.h>
int main ()
{
FILE * pFile;
pFile=fopen ("unexist.ent","rb");
if (pFile==NULL)
perror ("The following error occurred");
else
fclose (pFile);
return 0;
}
|
If the file
unexist.ent does not exist, something similar to this Output is expected to be the program output:
The following error occurred: No such file or directory
See also
- clearerr
- Clear error indicators (function)
- ferror
- Check error indicator (function)