errno

Can you give examples of functions setting errno?

What are some standard functions that set it?
closed account (18hRX9L8)
I am not sure what that is. Is there a website where I can read about errno?
Last edited on
http://www.cplusplus.com/reference/cerrno/errno/

I have never actually seen it used though, so I can't help more than that.
Last edited on
closed account (18hRX9L8)
Some examples I took from the reference.

1
2
3
4
5
6
7
8
9
10
11
12
13
/* 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:

http://www.cplusplus.com/reference/cstdio/perror/
http://www.cplusplus.com/reference/cstring/strerror/
Topic archived. No new replies allowed.