/* strerror example : error list */
#include <stdio.h>
#include <string.h>
#include <errno.h>
int main ()
{
FILE * pFile;
pFile = fopen ("unexist.ent","r");
if (pFile == NULL)
printf ("Error opening file unexist.ent: %s\n",strerror(errno));
return 0;
}
This returns:
Error opening file unexist.ent: No such file or directory.
In this case, strerror(errno) is the string version of the error.
1 2 3 4 5 6 7 8 9 10 11 12 13
/* perror example */
#include <stdio.h>
int main ()
{
FILE * pFile;
pFile=fopen ("unexist.ent","rb");
if (pFile==NULL)
perror ("The following error occurred");
else
fclose (pFile);
return 0;
}
The output is:
The following error occured: No such file or directory.
In this case, perror ("str") is just a printf but it include the error message at the end.
These are the only two functions of errno, and it depends which one you want to use for what reason. You can read up more at: