By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,819 Members | 1,185 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,819 IT Pros & Developers. It's quick & easy.

errno

P: 4
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
Feb 23 '07 #1
Share this Question
Share on Google+
4 Replies


DeMan
100+
P: 1,806
I'll admit I've not used it before, but there appear to be many references on the web that may be helpful.....
Feb 23 '07 #2

Banfa
Expert Mod 5K+
P: 8,916
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.
Feb 23 '07 #3

P: 4
Thanks a lot !
Feb 24 '07 #4

Expert 100+
P: 1,510
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
Expand|Select|Wrap|Line Numbers
  1.     if ((in_file = fopen(filename, "r")) == NULL)          /* open input file */
  2.         {
  3.         printf("\nUnable to open file '%s': %s", filename, strerror(errno));
  4.         return 1;                                              /* open failed */
  5.         }
  6.  
a run gave
Enter name of input file ? file1
Unable to open file 'file1': No such file or directory
Feb 24 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.