|
-
March 28th, 2005, 02:50 PM
#1
strings to floating point number
I found this subset of code in the frequently asked questions section and decided to try it out. I found a couple of problems with it and was wondering if anyone can help me understand.
const char* str_int = "777";
const char* str_float = "333.3";
int i;
float f;
if(EOF == sscanf(str_int, "%d", &i)){
//error
}
if(EOF == sscanf(str_float, "%f", &f)){
//error
}
First of all, if the string value is "hello", sscanf returns 0, not EOF. So I don't understand why the author recommends checking for EOF. If the input string is not a number, then I want to take the error path.
Second, if I define the variable f to be of type double instead of float, the sscanf function will fail. Why? I thought double is a float but just twice the size. I searched in the MSDN for format specifiers and found nothing to indicate a special specifier for a double. Does anyone know what is going on here? When I say fail, I mean that the function returns 0 and the variable f will be garbage as if sscanf refuses to even try to convert the string to a double. However, it works great as long as the variable f is declared as a float.
Best Regards,
Shawn Fox
-
March 28th, 2005, 03:04 PM
#2
Re: strings to floating point number
Please use strtod() to convert from a string to a floating point value. The sscanf() function has a variety of pitfalls. There was a thread just last week on why sscanf() is a function that should not be used, and there are much better replacements (such as strtod).
Regards,
Paul McKenzie
-
March 28th, 2005, 03:21 PM
#3
Re: strings to floating point number
Thanks, I guess you're right. strtod will work fine for me. I sure wish someone would update that FAQ topic with new info. That led me pretty far down the wrong path.
Best Regards,
Shawn
-
March 28th, 2005, 04:21 PM
#4
Re: strings to floating point number
for string -> float (double), I personally use atof()
for float -> string I use sprintf()
just how I do it though.
-
March 28th, 2005, 04:29 PM
#5
Re: strings to floating point number
Thanks. I have used atof and atoi before. However, in this case it is critical that I know if the string can be completely converted. I need to know if the string is something like this, "hello" as opposed to "35.6", or "35 dollars".
My function needs to perform some error logic if the function cannot convert the entire input string. strtol and strod both provide that functionality. With atoi and atof, you had better know that the input string is completely convertable or ensure that it is not critical if it cannot be. You know what I mean? Also, atoi and atof return 0 for different situations making it impossible to use these functions if you need to do any error testing to determine if the whole string was converted or not.
Thanks for all the help. I definitely found what I was looking for.
Best Regards,
Shawn
-
March 29th, 2005, 03:37 AM
#6
Re: strings to floating point number
In addition, you might want to take a look at the following FAQ...
-
March 30th, 2005, 11:41 AM
#7
Re: strings to floating point number
Yes, I've seen it. That was the FAQ I referred to in the first post. It is incorrect and needs to be changed. The information on sscanf does not make sense at all. The FAQ makes it sound like scanf will detect strings that aren't numbers and that isn't true. I've tried it numerous ways and have never seen sscanf return EOF.
Shawn
-
March 30th, 2005, 03:13 PM
#8
Re: strings to floating point number
Without attempting to justify what's in the FAQ, from MSDN for "scanf":
The return value is EOF for an error or if the end-of-file character or the end-of-string character is encountered in the first attempt to read a character.
And I think to read a "double" value your format would have to be "%Lf"
Regards
Robert Thompson
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|