errno

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • wellwellsti
    New Member
    • Feb 2007
    • 4

    errno

    I only read 2 c++ book and none explain errno because its a c "thing"

    i dont know what is it and i dont know how to use it, but bsd socket use errno for connect() int reply

    somebody could give me a little tutorial

    thx
  • DeMan
    Top Contributor
    • Nov 2006
    • 1799

    #2
    I'll admit I've not used it before, but there appear to be many references on the web that may be helpful.....

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      errno translates to an expression that has a type of int and is declared in errno.h along with #defines for common values.

      It is used by some of the standard library functions and is set when an error occurs during the function call to provide extra information about what the error was (a bit like the Windows function GetLastError() if you are familiar with that).

      An example of a function that uses it is strtoul which returns a fixed error value (MAX_ULONG) but sets errno to indicate exactly what error occured.

      If you are going to use errno then it is important to set it's value to 0 before you call the function you may wish to test it after otherwise it could contain a value from a previous function call.

      Comment

      • wellwellsti
        New Member
        • Feb 2007
        • 4

        #4
        Thanks a lot !

        Comment

        • horace1
          Recognized Expert Top Contributor
          • Nov 2006
          • 1510

          #5
          as explained by Banfa when an error occurs many of functions set up an integer error number in errno which is declared in <errno.h> - a usful function is strerror in <string.h> which given errno as a parameter returns a pointer to a corresponding implementation defined string which can then be printed, e.g. the following calls strerror() if the fopen() fails
          Code:
              if ((in_file = fopen(filename, "r")) == NULL)          /* open input file */
                  {
                  printf("\nUnable to open file '%s': %s", filename, strerror(errno));
                  return 1;                                              /* open failed */
                  }
          a run gave
          Enter name of input file ? file1
          Unable to open file 'file1': No such file or directory

          Comment

          Working...