Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Borland C++ 2.x ( with Turbo C ) - <b>strerror() save system error message</b> http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
 strerror()              Save System Error Message

 #include   <string.h>                   Required for declarations only

 char       *strerror(errnum);
 int        errnum;                      Error number

    strerror() returns a pointer to a system error message if 'errnum' is
    non-zero.

       Returns:     If 'errnum' is non-zero, a pointer to the most
                    recently generated system error message is returned.
                    The string is terminated by a null character. The
                    error number is stored in 'errno' (defined in
                    <errno.h>). strerror() accesses the error message by
                    using the 'errno' value as an index to 'sys_errlist',
                    which holds the system error messages.


         Notes:     For accurate results, call strerror() immediately
                    after a routine returns to avoid 'errno' being
                    overwritten by subsequent routine calls.

                    Unlike perror(), strerror() does not actually print
                    any messages.  The program is responsible for
                    printing the returned message, as shown in the
                    example below.

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

    This example tries to open a file and prints an error message and
    exits if unable to do so.

           #include <string.h>
           #include <errno.h>             /* for errno */
           #include <stdio.h>             /* for printf*/
           #include <fcntl.h>             /* constants defined */
           #include <sys\stat.h>          /* constants defined */
           #include <io.h>                /* for open */

           int fh;

           main()
           {
              int errnum;

               if ((fh = open("invoice.dat",O_RDWR)) == -1) {
                  errnum = errno;
                  printf(_strerror(errnum));
                  exit(1);
               }
           }


See Also: perror() _strerror()

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