Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > errno

Reply
Thread Tools

errno

 
 
Bill Cunningham
Guest
Posts: n/a
 
      03-09-2012
I have been trying to figure out how to use errno instead of having to
turn to perror. Do you just include errno.h in your source or use code
something like this...and this is what I am trying to understand.

if (errno==-1)
fprintf(stderr,"%d\n",errno);

Bill


 
Reply With Quote
 
 
 
 
Bill Cunningham
Guest
Posts: n/a
 
      03-09-2012
Bill Cunningham wrote:
> I have been trying to figure out how to use errno instead of
> having to turn to perror. Do you just include errno.h in your source
> or use code something like this...and this is what I am trying to
> understand.
> if (errno==-1)
> fprintf(stderr,"%d\n",errno);
>
> Bill


Ok I think I might have caught on now.
Thanks anyway.

Bill


 
Reply With Quote
 
 
 
 
Kiru Sengal
Guest
Posts: n/a
 
      03-09-2012
On Mar 8, 8:54*pm, "Bill Cunningham" <(E-Mail Removed)> wrote:
> * * I have been trying to figure out how to use errno instead of having to
> turn to perror. Do you just include errno.h in your source or use code
> something like this...and this is what I am trying to understand.
>
> if (errno==-1)
> fprintf(stderr,"%d\n",errno);
>
> Bill


errno is never negative
Yes you can test it in if statements like that

Including errno.h gives you access to symbolic constants mapped to the
positive integers returned by errno.

So your if statements are better off being like: if (errno ==
ERANGE ) ...

look up strerror too
 
Reply With Quote
 
Don Y
Guest
Posts: n/a
 
      03-09-2012
Hi Bill,

On 3/8/2012 6:54 PM, Bill Cunningham wrote:
> I have been trying to figure out how to use errno instead of having to
> turn to perror. Do you just include errno.h in your source or use code
> something like this...and this is what I am trying to understand.
>
> if (errno==-1)
> fprintf(stderr,"%d\n",errno);


Include <errno.h> in your source file(s).

Before EVERY library call that *can* (potentially)
modify errno, explicitly SET IT TO ZERO.

After *each* such library call, test errno for
a non-zero value (indicative of an error).

The actual error values that are reported will
vary with your execution environment, etc.

If you are looking for a specific type of error
(e.g., ERANGE), make sure you refer to it symbolically
in your code since the actual values for these errors
will vary from system to system.

(N.B. errno *itself* might be a macro in some environments!)
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      03-09-2012
Don Y <(E-Mail Removed)> writes:
> On 3/8/2012 6:54 PM, Bill Cunningham wrote:
>> I have been trying to figure out how to use errno instead of having to
>> turn to perror. Do you just include errno.h in your source or use code
>> something like this...and this is what I am trying to understand.
>>
>> if (errno==-1)
>> fprintf(stderr,"%d\n",errno);

>
> Include <errno.h> in your source file(s).
>
> Before EVERY library call that *can* (potentially)
> modify errno, explicitly SET IT TO ZERO.
>
> After *each* such library call, test errno for
> a non-zero value (indicative of an error).


Do so only if the function has returned a value indicating that it
failed. A function may set errno to a non-zero value even if it
succeeded (typically because some function that it called did so).

A non-zero errno value tells you why a call failed; it doesn't tell
you *whether* it failed.

> The actual error values that are reported will
> vary with your execution environment, etc.
>
> If you are looking for a specific type of error
> (e.g., ERANGE), make sure you refer to it symbolically
> in your code since the actual values for these errors
> will vary from system to system.
>
> (N.B. errno *itself* might be a macro in some environments!)


Note that surprisingly few functions in the standard library are
required to set errno on failure.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
errno 22 instead of errno 2 Glenn Linderman Python 0 01-28-2009 07:52 AM
&errno, sizeof errno viza C Programming 20 09-14-2008 09:53 PM
%PIX-3-213001: PPTP control daemon socket io read error, errno = -2043674623 jcharth@hotmail.com Cisco 1 10-20-2005 02:57 PM
errno, etymology thereof unspammable@gmail.com C++ 3 03-04-2005 01:33 PM
conversion: errno => exception Markus Elfring C++ 13 01-05-2005 09:38 PM



Advertisments