strerror


strerror

Obtains a string that describes a given error

 #include <string.h> char *strerror ( int errornumber  ); 

The strerror( ) function returns a pointer to an error message string that corresponds to the specified error number. The argument value is usually that of the errno variable, but can be any integer value. The string pointed to by the return value of strerror( ) may change on successive strerror( ) calls.

Example

 FILE *fp; char msgbuf[1024] = { '\0' }; /* Open input file: */ if (( fp = fopen( "nonexistent", "r" )) == NULL) {   int retval = errno;   snprintf( msgbuf, sizeof(msgbuf),             "%s:  file %s, function %s, line %d: error %d, %s.\n",             argv[0], _  _FILE_  _, _  _func_  _, _  _LINE_  _, retval,             strerror( retval ));   fputs( msgbuf, stderr );   return retval; } 

This error-handling block prints the following message:

 ./strerror:  file strerror.c, function main, line 17: error 2, No such file or directory. 

See Also

perror( )



C(c) In a Nutshell
C in a Nutshell (In a Nutshell (OReilly))
ISBN: 0596006977
EAN: 2147483647
Year: 2006
Pages: 473

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net