473,396 Members | 2,021 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Help with strerror(errno)

Is errno something that I have to modify, or is it automatically modified when certain commands are executed?

So if I just did something, and then printed strerror(errno) would it know what error to print out?

Also - what happens if I have an empty strerror() ? Will it cause an error or what is its behavoir?

thanks!!
Aug 8 '08 #1
12 6702
Google is your friend. I typed "strerror" into google and the first page provides a nice overview of the function and answers your question nively. Then I entered "errno" and after looking at the third link (the IBM one) I once again had my answer.

Try Google.

Additionally, since I believe that errno.h is a linux header file I am assuming you are using linux. If that is the case then try going to the terminal and entering "man strerror". That should tell you what you want to know about strerror. As for errno, try the same thing (that would be the "man errno" command in this case).

Edward
Aug 8 '08 #2
As a side note, I have a feeling that one of the reasons that you didn't get a response sooner is because of the incredible ease in which your questions could be answered by using google. People are more willing to help those who have done their research.

Edward
Aug 8 '08 #3
As a side note, I have a feeling that one of the reasons that you didn't get a response sooner is because of the incredible ease in which your questions could be answered by using google. People are more willing to help those who have done their research.

Edward

I DID do my research in fact I spent the last two hours "googling" and reading all about it. I just had some questions in my head that are not clear to me...and I obvoiusly did not find answers to them OR I WOUDN'T HAVE POSTED THIS MESSAGE.
Aug 8 '08 #4
Savage
1,764 Expert 1GB
I DID do my research in fact I spent the last two hours "googling" and reading all about it. I just had some questions in my head that are not clear to me...and I obvoiusly did not find answers to them OR I WOUDN'T HAVE POSTED THIS MESSAGE.
Calm down.Edward didn't wanted to offend you in any way.He just told you what 'usually happens'.If you have done some research you should have explicitly said that in your post.


So if I just did something, and then printed strerror(errno) would it know what error to print out?

thanks!!
Yes,it would.



Also - what happens if I have an empty strerror() ? Will it cause an error or what is its behavoir?
If NULL is passed it would show last error.
Aug 8 '08 #5
Well, if you see the first link under strerror on google you will see that the method has parameters. You must pass the parameters, you may be able to pass Null but you must pass something.

As for errno. The page on IBM's website that I referred to shows examples using strerror and errno. In fact, it talks all about error handling and how to do it. The example code it shows does not change errno, so I would say that is a good indicator you don't have to.

If you read all my post (I don't know if you did or didn't) AND you are running linux then I would really take a look at the man pages. They do an amazing job of explaining functions and commands. Additionally, they tend to tell you what behavior to expect from different functions if NULL is passed. Even if you aren't running linux looking at the man pages on line could be a very good way to get a feel for the functions though the behavior could change drastically under windows.

Edward
Aug 8 '08 #6
donbock
2,426 Expert 2GB
dissectcode:

errno and strerror() are both part of Standard C (they are not limited to only Linux). Why don't you tell us a little of what you've learned about them from your research. For instance,
> What header file(s) need to be included for them to work properly?
> How do you use errno to determine whether or not there has been an error?
> If there was an error, how do you use errno to determine what the error was?
> In what manner does strerror() provide the error message text?

My guess is that you're trying this in a single-threaded application. FYI, in a multi-threaded application using errno gets a lot trickier. If you have lots of threads running simultaneously, and they're all calling library functions, then it is very difficult for you to know which thread (and hence which library function) put the error code in errno. Be glad you don't have to worry about that!

Good luck,
Don
Aug 8 '08 #7
[quote=donbock]dissectcode:
Why don't you tell us a little of what you've learned about them from your research. For instance,
> What header file(s) need to be included for them to work properly?
> How do you use errno to determine whether or not there has been an error?
> If there was an error, how do you use errno to determine what the error was?
> In what manner does strerror() provide the error message text?

ok just because you need proof that I DID my research I am answering this without even going back to google for this information:
1. the errno.h header file is needed to use errno and string.h is needed for strerror()
2. to use errno, you can print it in a printf statement usign %s format specifier. i am sure you can also use that pointer that was returned to print out custom errors, or do something as a result.
3. you can evaluate the error code by matching the pointer that was returned, to the list of error codes that C supplies (specific for strerror i guess?)
4. sterror() provides the pointer to the error from the list.
Aug 8 '08 #8
donbock
2,426 Expert 2GB
Thanks, I didn't want to get in trouble with the moderator.

2. to use errno, you can print it in a printf statement usign %s format specifier. i am sure you can also use that pointer that was returned to print out custom errors, or do something as a result.
Actually, errno has type int, so you should use "%d" format in printf.

3. you can evaluate the error code by matching the pointer that was returned, to the list of error codes that C supplies (specific for strerror i guess?)
As mentioned above, errno has type int so don't compare it to any pointers. The C Standard says "The value of errno is zero at program startup, but is never set to zero by any library function."

4. sterror() provides the pointer to the error from the list.
The function prototype for strerror() is
Expand|Select|Wrap|Line Numbers
  1. char *strerror(int errnum);
so you should be able to do something like this...
Expand|Select|Wrap|Line Numbers
  1. if (errno != 0) {
  2.     printf("%s\n",strerror(errno));
  3.     errno = 0;
  4.     }
Your compiler might implement errno as a global variable, but it also might do something extremely tricky. However your compiler implements errno, the C Standard requires that errno "expands to a modifiable lvalue". A 'modifiable lvalue' is something that can go on the left side of an equals sign, that's why you're able to reset errno to zero.

The type of errno, the fact that it starts at zero and gets set to various nonzero values by errors, and that it is a modifiable lvalue should have been explained in the man page for errno and/or for errno.h.

Cheers,
Don
Aug 8 '08 #9
donbock
2,426 Expert 2GB
Also - what happens if I have an empty strerror() ? Will it cause an error or what is its behavoir?
If by "empty strerror()" you mean that you don't pass any argument, then you should get a compiler error if the strerror() prototype is in scope.

If by "empty strerror()" you mean the argument you pass is errno but no error has occurred yet (ie, errno is still zero), then I'm not sure what happens since the C Standard is silent on this detail. Since zero is a well-known value for errno it seems reasonable that strerror() would return some sort of "No error" message. You can avoid this issue by only callling strerror() when errno is nonzero.

If by "empty strerror()" you mean the argument you pass is errno but it has some garbage nonzero value that is unknown to strerror, then I'm not sure what happens since the C Standard is silent on this detail. I would be disappointed in the compiler-writer if strerror() did something stupid in this case, it seems reasonable that it would return some sort of "Unknown error" message. You can avoid this issue by being careful to never store any value into errno other than zero.

If by "empty strerror()" you mean the argument passed is NULL, then there would be a compiler error if the prototype was in scope because NULL isn't a suitable int.

Cheers,
Don
Aug 8 '08 #10
If by "empty strerror()" you mean that you don't pass any argument, then you should get a compiler error if the strerror() prototype is in scope.

If by "empty strerror()" you mean the argument you pass is errno but no error has occurred yet (ie, errno is still zero), then I'm not sure what happens since the C Standard is silent on this detail. Since zero is a well-known value for errno it seems reasonable that strerror() would return some sort of "No error" message. You can avoid this issue by only callling strerror() when errno is nonzero.

If by "empty strerror()" you mean the argument you pass is errno but it has some garbage nonzero value that is unknown to strerror, then I'm not sure what happens since the C Standard is silent on this detail. I would be disappointed in the compiler-writer if strerror() did something stupid in this case, it seems reasonable that it would return some sort of "Unknown error" message. You can avoid this issue by being careful to never store any value into errno other than zero.

If by "empty strerror()" you mean the argument passed is NULL, then there would be a compiler error if the prototype was in scope because NULL isn't a suitable int.

Cheers,
Don

Quick question: If I don't pass an argument ie strerror(); isn't what is passed technically NULL?
thanks!
Aug 14 '08 #11
arnaudk
424 256MB
Quick question: If I don't pass an argument ie strerror(); isn't what is passed technically NULL?
thanks!
No. If a function expects an argument then you have to give it one (unless there is an overloaded version which takes no arguments which is possible in C++ but not C).
Aug 14 '08 #12
JosAH
11,448 Expert 8TB
If by "empty strerror()" you mean the argument you pass is errno but it has some garbage nonzero value that is unknown to strerror, then I'm not sure what happens since the C Standard is silent on this detail. I would be disappointed in the compiler-writer if strerror() did something stupid in this case, it seems reasonable that it would return some sort of "Unknown error" message. You can avoid this issue by being careful to never store any value into errno other than zero.
The C99 Standard is not completely silent about any arbitrary value:

[#2] The strerror function maps the number in errnum to a
message string. Typically, the values for errnum come from
errno, but strerror shall map any value of type int to a
message.
Read the last phrase of the last sentence.

kind regards,

Jos
Aug 14 '08 #13

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

Similar topics

4
by: Neo | last post by:
Hi friends where will I get error descriptions for numorous errors I am getting on my pgm?
6
by: Michael McGarry | last post by:
Hi, How do I interpret errno from fopen()? errno = 24 in my case. Thanks for any help, Michael
12
by: manochavishal | last post by:
Hi, I am having strange problem in my Program. I cannot paste the whole program as it is huge so just pasting the lines i think are necessary. I am passing a integer array pointer to a...
3
by: Piripiccio | last post by:
Hello , please give me a little minute for this problem the first thing ..... my english is very bad I wrote a little program that using a socket pair with 2 process Padre (Father) and Figlio...
5
by: Gregory Piñero | last post by:
Hi Guys, I'm sure this is documented somewhere, I just can't locate it. Say I have this code: try: myfile=file('greg.txt','r') except IOError, error: #now psuedo code because this is what...
5
by: weidongtom | last post by:
Hi, I tried to implement the Universal Machine as described in http://www.boundvariable.org/task.shtml, and I managed to get one implemented (After looking at what other's have done.) But when I...
9
by: weidongtom | last post by:
Hi, I've written the code that follows, and I use the function add_word(), it seems to work fine *before* increase_arrays() is called that uses realloc() to allocate more memory to words. But...
1
by: orehian | last post by:
Construct a one-time password system. · Write a server code and a client code. The server code takes as input a username and a one-time password from the client and then sends a message...
7
by: Richard Cranium | last post by:
Trying to implement a simple rrd. Code below. When calling rrd_write, 8 successful times in a row, the 9th one causes this to happen: Program received signal EXC_BAD_ACCESS, Could not access...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.