Syntax
#include <string.h> char *_strerror(char *string);Description
Your string message can be a maximum of 94 bytes.
Unlike perror, _strerror by itself does not print a message. To print the message returned by _strerror to stdout, use a printf statement similar to the following:
if ((access("datafile",2)) == -1)
printf(stderr,_strerror(NULL));
You could also print the message to stderr with an fprintf statement.
To produce accurate results, call _strerror immediately after a library function returns with an error. Otherwise, subsequent calls might write over the errno value.
If string is equal to NULL, _strerror returns a pointer to a string containing the system error message for the last library call that produced an error, ended by a new-line character (\n).
If string is not equal to NULL, _strerror returns a pointer to a string containing:
This example shows how _strerror can be used with the fopen function.
#include <string.h>#include <stdio.h>
#define INFILE "_strerro.in"
#define OUTFILE "_strerro.out"
int main(void)
{
FILE *fh1,*fh2;
fh1 = fopen(INFILE, "r");
if (NULL == fh1)
/* the error message goes through stdout not stderr */
printf(_strerror("Open failed on input file"));
fh2 = fopen(OUTFILE, "w+");
if (NULL == fh2)
printf(_strerror("Open failed on output file"));
else
printf("Open on output file was successful.\n");
if (fh1 != NULL)
fclose(fh1);
if (fh2 != NULL)
fclose(fh2);
remove(OUTFILE);
return 0;
/****************************************************************************
The output should be:
Open failed on input file: The file cannot be found.
Open on output file was successful.
****************************************************************************/
}
Related Information