_strerror -- Set Pointer to System Error String

Format

#include <string.h>
char *_strerror(char *string);

Language Level: Extension
_strerror tests for system error. It gets the system error message for the last library call that produced an error and prefaces it with your string message.

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(_strerror(NULL));

You could also print the message to stderr with an fprintf statement.

if ((access("datafile",2)) == -1)
fprintf(stderr,_strerror(NULL));

To produce accurate results, call _strerror immediately after a library function returns with an error. Otherwise, subsequent calls might write over the errno value.

Return 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:

Example
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.
   ************************************************************/
}



clearerr -- Reset Error Indicators
ferror -- Test for Read/Write Errors
perror -- Print Error Message
strerror -- Set Pointer to Runtime Error Message
<string.h>