perror

Writes a short error message to stderr describing the current value of errno.

Format

#include  <stdio.h>

void perror  (const char *str);

Argument

str
Usually the name of the program that caused the error.

Description

This function uses the error number in the external variable errno to retrieve the appropriate locale-dependent error message. The function writes out the message as follows: str (a user-supplied prefix to the error message), followed by a colon and a space, followed by the message itself, followed by a new-line character.

See the description of errno in Chapter 4 for a list of possible errors.

See also strerror in this section.

Example

    #include <stdio.h>
    #include <stdlib.h>
    
    main(argc,argv)
    int argc;
    char *argv[];
    {
     FILE *fp;
     int status;
     int total_recs = -1;
    
     fp = fopen(argv[1],"r");                   /* Open an input file. */
     if (fp == NULL)
      {
    
      /*
       * If the fopen call failed, perror prints out a diagnostic:
       *
       *    "open: <error message>"
       *    This error message provides a diagnostic explaining
       *    the cause of the failure.
       *
       */
      perror("open");
      exit(1);
      }
     }
    


Previous Page | Next Page | Table of Contents | Index