|
|
#include <string.h>char *strerror(errnum) int errnum;
#include <string.h> #include <errno.h> #include <fcntl.h> #include <sys/types.h> #include <sys/stat.h>The program shown above tries to open files xxxx and yyyy. If an error occurs opening xxxx, the variable errnum is set to the errno value returned by open. Other code that may alter the errno value is then executed. Later, the saved errno value in errnum is checked and, if nonzero, an error message assigned to it by strerror is printed. If file xxxx does not exist, the example prints the following message:extern int errno; int errnum; int fh1, fh2;
main() { errnum=0; if ((fh1=open("xxxx",O_RDONLY)) == -1) errnum=errno; fh2=open("yyyy",O_RDONLY);
/* Other code that may set the errno value.*/ if (errnum != 0) printf(strerror(errnum)); }
No such file or directory