473,395 Members | 1,668 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,395 software developers and data experts.

atof() failure case

Hello,
Am trying to convert a string to float. Am using atof() for that purpose.
But the return value for atof is same for the string "0.0" and for some
invalid input "Invalid". Can any body suggest me the way to differentiate an
invalid float number string and a 0.0.
TIA,
Sreekanth.
Nov 14 '05 #1
6 4460
"Sreekanth" <sr*******@yahoo.com> wrote:
Hello,
Am trying to convert a string to float. Am using atof() for that purpose.
But the return value for atof is same for the string "0.0" and for some
invalid input "Invalid". Can any body suggest me the way to differentiate an
invalid float number string and a 0.0.


Instead of atof use strtod.

Regards
--
Irrwahn Grausewitz (ir*******@freenet.de)
welcome to clc: http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc OT guide : http://benpfaff.org/writings/clc/off-topic.html
Nov 14 '05 #2
Thanks, But it also returns 0 in both cases.. I want to differentiate an
invalid string and a Zero string in a string to float conversion mechanism.

"Irrwahn Grausewitz" <ir*******@freenet.de> wrote in message
news:72********************************@4ax.com...
"Sreekanth" <sr*******@yahoo.com> wrote:
Hello,
Am trying to convert a string to float. Am using atof() for that purpose.
But the return value for atof is same for the string "0.0" and for some
invalid input "Invalid". Can any body suggest me the way to differentiate aninvalid float number string and a 0.0.


Instead of atof use strtod.

Regards
--
Irrwahn Grausewitz (ir*******@freenet.de)
welcome to clc: http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc OT guide : http://benpfaff.org/writings/clc/off-topic.html

Nov 14 '05 #3
Sreekanth <sr*******@yahoo.com> wrote:
"Irrwahn Grausewitz" <ir*******@freenet.de> wrote in message
news:72********************************@4ax.com...
"Sreekanth" <sr*******@yahoo.com> wrote:
>Hello,
>Am trying to convert a string to float. Am using atof() for that purpose.
>But the return value for atof is same for the string "0.0" and for some
>invalid input "Invalid". Can any body suggest me the way to differentiate an >invalid float number string and a 0.0.
Instead of atof use strtod.

Thanks, But it also returns 0 in both cases.. I want to differentiate an
invalid string and a Zero string in a string to float conversion mechanism.


Well, that's why you should use strtod():

double strtod(const char *nptr, char **endptr);

If you got an illegal argument *endptr and nptr are identical after
the call of the function. On legal input what endptr points to is
a pointer to the part of the input string where the conversion
stopped and it differs from nptr.
Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #4
Thanks Gentleman.. Working..

<Je***********@physik.fu-berlin.de> wrote in message
news:c5************@uni-berlin.de...
Sreekanth <sr*******@yahoo.com> wrote:
"Irrwahn Grausewitz" <ir*******@freenet.de> wrote in message
news:72********************************@4ax.com...
"Sreekanth" <sr*******@yahoo.com> wrote:
>Hello,
>Am trying to convert a string to float. Am using atof() for that purpose. >But the return value for atof is same for the string "0.0" and for some >invalid input "Invalid". Can any body suggest me the way to
differentiate an
>invalid float number string and a 0.0.

Instead of atof use strtod.
Thanks, But it also returns 0 in both cases.. I want to differentiate an
invalid string and a Zero string in a string to float conversion

mechanism.
Well, that's why you should use strtod():

double strtod(const char *nptr, char **endptr);

If you got an illegal argument *endptr and nptr are identical after
the call of the function. On legal input what endptr points to is
a pointer to the part of the input string where the conversion
stopped and it differs from nptr.
Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de

Nov 14 '05 #5
Sreekanth wrote:

Am trying to convert a string to float. Am using atof() for that
purpose. But the return value for atof is same for the string "0.0"
and for some invalid input "Invalid". Can any body suggest me the
way to differentiate an invalid float number string and a 0.0.


strtod();

--
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 14 '05 #6
In <c5**********@news.mch.sbs.de> "Sreekanth" <sr*******@yahoo.com> writes:
Thanks, But it also returns 0 in both cases..
It also provides enough clues to make the disambiguation trivial.
I want to differentiate an
invalid string and a Zero string in a string to float conversion mechanism.
Either use the clues provided by strtod, or use atof and do some minimal
parsing of the string yourself, when the result of the conversion is 0.

Dan
"Irrwahn Grausewitz" <ir*******@freenet.de> wrote in message
news:72********************************@4ax.com.. .
"Sreekanth" <sr*******@yahoo.com> wrote:
>Hello,
>Am trying to convert a string to float. Am using atof() for that purpose.
>But the return value for atof is same for the string "0.0" and for some
>invalid input "Invalid". Can any body suggest me the way to differentiatean >invalid float number string and a 0.0.


Instead of atof use strtod.


--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Drew | last post by:
Hi: I'm having trouble getting atof() to accurately convert "3.1" Any ideas? Thank you,
6
by: Sreekanth | last post by:
Hello, Am trying to convert a string to float. Am using atof() for that purpose. But the return value for atof is same for the string "0.0" and for some invalid input "Invalid". Can any body...
15
by: XZ | last post by:
Hi everyone, this is really confusing to me: #include <stdio.h> main(int argc, char **argv) { printf("argv = %f\n",(double)atof(argv)); printf("argv = %d\n\n",atoi(argv)); } $ a.out a argv...
20
by: Trond Valen | last post by:
Hi! Stupid atof, it returns 0.0 when it tries to parse something like "fish". So I don't know whether the number was really 0 or a string that couldn't be parsed. Is there a better way to do...
21
by: oksuresh | last post by:
Hi talents, I have noticed that atof() function approximates the string I pass to it. when I use atof() , as atof(" 184.64") and it returns 184.63999999999 But I would like to have...
5
by: tjay | last post by:
Hi. I wrote some code using sprintf and atof to store a double as a string of fixed length and to convert it back to a double variable. The string is stored in a char buffer global variable. I'm...
14
by: sharmaharish | last post by:
I need a conversion function that converts values from string to a particular type. For this I have a template function that looks like this ... template<class T> T value(const string& s) {...
5
by: lcw1964 | last post by:
Greetings again, I will burden the group with yet another tenderfoot question, but since conscientious googling hasn't yield a lucid answer I thought I would risk the shortcut of asking here...
2
by: allexander | last post by:
Hello ! I need to convert a string like string number = "1235646.5678" to any floating point number x (double x or float x) I have used earlier the function atof()
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.