需要帮助strtof返回值 [英] Needed help for strtof return value

查看:83
本文介绍了需要帮助strtof返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的小组


我编写了一个文件转换程序,它使用strtof将

文本字符串转换为浮点数。除了我的错误

消息之外,它按我的意图工作。据我所知,如果转换成功,则strtof返回转换后的

值为0。失败了。当这个

转换失败时,我的程序会向error.log

文件发送一条错误消息。这个问题是有时strtof会转换一个

文本字符串,例如0.000000。浮动。转换是成功的但是当程序去测试

转换是否成功时,因为返回的值等于0。它

发出错误信息。这可能会发生数百甚至数千个b $ b b次。因此,检查所有

消息以查看转换是否失败是不可行的。有没有人有任何想法

我如何测试我的

情况的成功转换。

问候


Marcus D. Jacobs

解决方案

文章< 96 ************* *************@posting.google.com>,Marcus Jacobs写道:

亲爱的小组

我写了一个文件转换使用strtof将文本字符串转换为浮点数的程序。除了我的错误消息之外,它按我的意图工作。我的理解是,如果转换成功,则strtof返回转换后的值,该值为0。失败了。当这个转换失败时,我的程序会向error.log
文件发送一条错误消息。这个问题是有时strtof会转换一个
文本字符串,例如0.000000。浮动。转换成功但是当程序去测试
转换是否成功时,因为返回的值等于0。它发出错误信息。这可能会发生数百甚至数千次。因此,检查所有消息以查看转换是否失败是不可行的。有没有人有任何想法
我如何测试我的
情况的成功转换。



当然:比较您尝试转换的字符串0.000000。如果你可以在点之前和之后有任意数量的零,你可以这样做:


int is_zero(const char * str)

{

int i;

int found_dot = 0;

for(i = 0; str [i] ; i ++)

{

if(str [i] ==''。'')

{

if(found_dot)

返回-1; / *错误* /

found_dot ++;

继续;

}

if(str [i]!= ''0'')

返回(0);

}


返回(1);

}


HTH


rlc


-

监狱:只是另一种解释语言

只是:监狱使用愚蠢的条款


加入讨论这种语言的定义
ja *********** @ lists.sourceforge.net http://jail-ust.sourceforge.net

(发送邮件到 ja ********** ***********@lists.sourceforge.net


ma ******* @ hotmail.com (Marcus Jacobs)写道

新闻:96 ********* *****************@posting.google.c om:

亲爱的小组

我编写了一个文件转换程序,它使用strtof将文本字符串转换为浮点数。除了我的错误消息之外,它按我的意图工作。我的理解是,如果转换成功,则strtof返回转换后的值,该值为0。失败了。当这个转换失败时,我的程序会向error.log
文件发送一条错误消息。这个问题是有时strtof会转换一个
文本字符串,例如0.000000。浮动。转换成功但是当程序去测试
转换是否成功时,因为返回的值等于0。它发出错误信息。




返回零并不是strtof失败时唯一的做法。阅读关于第二个参数的

文档。然后,在你的代码中进行适当的错误检查

而不是只进行中途的错误。


思南。


ma*******@hotmail.com (Marcus Jacobs)写在

news:96 ************************** @ posting.google.c om:

亲爱的小组

我编写了一个文件转换程序,它使用strtof将
文本字符串转换为浮点数。除了我的错误消息之外,它按我的意图工作。我的理解是,如果转换成功,则strtof返回转换后的值,该值为0。失败了。当这个转换失败时,我的程序会向error.log
文件发送一条错误消息。这个问题是有时strtof会转换一个
文本字符串,例如0.000000。浮动。转换成功但是当程序去测试
转换是否成功时,因为返回的值等于0。它发出错误信息。




返回零并不是strtof失败时唯一的做法。阅读关于第二个参数的

文档。然后,在你的代码中进行适当的错误检查

而不是只进行中途的错误。


思南。


Dear Group

I have written a file conversion program that uses strtof to convert
text strings to floats. It works as I intended except for my error
messages. It is my understanding that strtof returns the converted
value if the conversion is successful a "0" upon failure. When this
conversion fails, my program sends an error message to my error.log
file. The problem with this is there are times when strtof converts a
text string such as "0.000000" to a float. The conversion is
successful but when the program goes to test whether or not the
conversion is successful, since the returned value is equal to "0" it
send out an error message. This can happen hundreds if not thousands
of times. Because of this, it is not feasible to check all of the
messages to see if the conversion failed. Does anyone have any ideas
as to how I can test for a successful strtof conversion for my
situation.
Regards

Marcus D. Jacobs

解决方案

In article <96**************************@posting.google.com >, Marcus Jacobs wrote:

Dear Group

I have written a file conversion program that uses strtof to convert
text strings to floats. It works as I intended except for my error
messages. It is my understanding that strtof returns the converted
value if the conversion is successful a "0" upon failure. When this
conversion fails, my program sends an error message to my error.log
file. The problem with this is there are times when strtof converts a
text string such as "0.000000" to a float. The conversion is
successful but when the program goes to test whether or not the
conversion is successful, since the returned value is equal to "0" it
send out an error message. This can happen hundreds if not thousands
of times. Because of this, it is not feasible to check all of the
messages to see if the conversion failed. Does anyone have any ideas
as to how I can test for a successful strtof conversion for my
situation.


Sure: compare the string you tried to convert with 0.000000. If you can have
any number of zeroes before and after the dot, you can do something like this:

int is_zero(const char * str)
{
int i;
int found_dot = 0;
for (i = 0; str[i]; i++)
{
if (str[i] == ''.'')
{
if (found_dot)
return -1; /* error */
found_dot++;
continue;
}
if (str[i] != ''0'')
return(0);
}

return(1);
}

HTH

rlc

--
Jail: Just Another Interpreted Language
Just: Jail Uses Silly Terms

Join the discussion on the definition of this language at
ja***********@lists.sourceforge.net http://jail-ust.sourceforge.net
(send mail to ja*********************@lists.sourceforge.net)


ma*******@hotmail.com (Marcus Jacobs) wrote in
news:96**************************@posting.google.c om:

Dear Group

I have written a file conversion program that uses strtof to convert
text strings to floats. It works as I intended except for my error
messages. It is my understanding that strtof returns the converted
value if the conversion is successful a "0" upon failure. When this
conversion fails, my program sends an error message to my error.log
file. The problem with this is there are times when strtof converts a
text string such as "0.000000" to a float. The conversion is
successful but when the program goes to test whether or not the
conversion is successful, since the returned value is equal to "0" it
send out an error message.



Returning zero is not the only thing strtof does upon failing. Read the
docs about the second parameter. Then, do a proper error check in your code
rather than one that only goes halfway.

Sinan.


ma*******@hotmail.com (Marcus Jacobs) wrote in
news:96**************************@posting.google.c om:

Dear Group

I have written a file conversion program that uses strtof to convert
text strings to floats. It works as I intended except for my error
messages. It is my understanding that strtof returns the converted
value if the conversion is successful a "0" upon failure. When this
conversion fails, my program sends an error message to my error.log
file. The problem with this is there are times when strtof converts a
text string such as "0.000000" to a float. The conversion is
successful but when the program goes to test whether or not the
conversion is successful, since the returned value is equal to "0" it
send out an error message.



Returning zero is not the only thing strtof does upon failing. Read the
docs about the second parameter. Then, do a proper error check in your code
rather than one that only goes halfway.

Sinan.


这篇关于需要帮助strtof返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆