Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Turbo C - <b>perror() print error message</b> http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
perror()                 Print Error Message

 #include   <stdlib.h>                   Required for declarations only

 void       perror(string);
 char       *string;                     User-supplied message

 int        errno;                       Error number
 int        sys_nerr;                    Number of system messages
 char       sys_errlist[sys_nerr];       Array of error messages

    perror() prints an error message to 'stderr'.  In order, perror()
    prints:

            the user-supplied string argument
            a colon
            system error message for last library call producing an error
            a new-line character

    System error messages are accessed through an array of messages,
    ordered by 'errno', called 'sys_errlist'.  The error number generated
    by the most recent system call is stored in the external variable,
    'errno' (which should be declared at the external level); 'errno'
    thus reflects the most recent error generated by a system library
    call in the current program.  perror uses errno as an index to
    sys_errlist to print the appropriate error message.  'sys_nerr'
    contains the maximum number of elements in 'sys_errlist'.

    Returns:    There is no return value.

      Notes:    Call perror() immediately after the system call returns
                with an error. Otherwise, the error number in 'errno' may
                be overwritten by an error generated by a subsequent
                system call.

  -------------------------------- Example ---------------------------------

         #include <fcntl.h>                /* constants defined */
         #include <sys\types.h>
         #include <sys\stat.h>             /* constants defined */
         #include <io.h>                   /* for open */
         #include <stdlib.h>


         int fhndl;

         main()
         {
             if ((fhndl = open("inp.dat",O_RDONLY)) == -1)
                 perror("unable to open file");
         }

See Also: clearerr() ferror() strerror()

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson