print, in stderr, the message associated with the value of errno
#include <stdio.h> void perror( const char *prefix );
The perror() function prints, on the file designated by stderr, the error message corresponding to the error number contained in errno. The perror() function writes first the string pointed to by prefix to stderr. This is followed by a colon (:), a space, the string returned by strerror(errno), and a newline character.
Because perror() uses the fprintf() function, errno can be set when an error is detected during the execution of that function. |
#include <stdio.h> void main() { FILE *fp; fp = fopen( "data.fil", "r" ); if( fp == NULL ) { perror( "Unable to open file" ); } }
ANSI
Safety: | |
---|---|
Interrupt handler | No |
Signal handler | No |
Thread | Yes |
clearerr(), errno, feof(), ferror(), strerror()
"Messages generated by perror()" in Appendix A: Implementation-Defined Behavior