strerror() Save System Error Message
#include <string.h> Required for declarations only
char *strerror(string);
char *string; User supplied message
strerror() generates customized error messages.
Returns: If 'string' is NULL, a pointer to the most recently
generated system error message is returned. The string
is terminated by a new-line character. The error number
is stored in 'errno' (defined in <stdlib.h>). strerror()
accesses the error message by using the 'errno' value as
an index to 'sys_errlist', which holds the system error
messages.
If 'string' is not NULL, strerror() returns a pointer to
a string which contains the customized error message
(supplied as the argument to 'strerror'), a colon, a
space, the last system error message, and a new-line
character.
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 a customized error
message and exits if unable to do so.
#include <string.h>
#include <stdio.h> /* for printf*/
#include <fcntl.h> /* constants defined */
#include <sys\types.h>
#include <sys\stat.h> /* constants defined */
#include <io.h> /* for open */
int fh;
main()
{
if ((fh = open("invoice.dat",O_RDWR)) == -1)
printf(strerror("ERROR OPENING INVOICE FILE"));
exit(1);
}
Seealso:
This page last updated on Fri Nov 30 10:48:32 MSK 2001
Copyright © 1992-2001, Vitaly Filatov, Moscow, Russia
Webmaster