Syntax
#include <stdio.h> void perror(const char *string);Description
perror prints an error message to stderr. If string is not NULL and does not point to a null character, the string pointed to by string is printed to the standard error stream, followed by a colon and a space. The message associated with the value in errno is then printed followed by a new-line character.
To produce accurate results, you should ensure that perror is called immediately after a library function returns with an error; otherwise, subsequent calls may alter the errno value.
There is no return value.
This example tries to open a stream. If fopen fails, the example prints a message and ends the program.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
   FILE *fh;
   if (NULL == (fh = fopen("myfile.mjq", "r"))) {
      perror("Could not open data file");
      abort();
   }
   return 0;
   /****************************************************************************
      The output should be:
      Could not open data file: The file cannot be found.
   ****************************************************************************/
}
Related Information