51Testing软件测试论坛

标题: 关于函数atof,巨晕中,求解。 [打印本页]

作者: jut984907    时间: 2010-12-20 17:35
标题: 关于函数atof,巨晕中,求解。
在监控tomcat时,虚拟内存可以取出来,字符串%s输出正确,但是%f 输出错误,例如:
lr_output_message("jvm s =   %s",lr_eval_string("{JVMFreeMemory}"));
lr_output_message("jvm f =  %f",atof(lr_eval_string("{JVMFreeMemory}")) );
-----
输出是: Action.c(38): jvm s =   53.85 ; Action.c(40): jvm f =  0.000000;
**************************
使用lr自带的 atof的例子:
-----
vuser_init() {

     float x;
     char *s = "7.2339 by these hilts or I am a villain else";

     x = atof(s);
     /* The %.2f formatting string limits the output to 2 decimal places */
     lr_output_message("%.2f", x);

     return 0;
}
-----
应该输出是 vuser_init.c(11): 7.23
但我的实际输出是 vuser_init.c(8): 1244136.00。
******************************
我的lr版本是9.1,tomcat是6。
哪位高手帮忙看看,先谢谢了~
作者: fish_yy    时间: 2010-12-21 05:06
应将atof()函数事先声明一下!

double atof(const char *string); /* Explicit declaration */

vuser_init() {

     float x;
     char *s = "7.2339 by these hilts or I am a villian else";

     x = atof(s);
     /* The %.2f formatting string limits the output to 2 decimal places */
     lr_output_message("%.2f", x);

     return 0;
}
作者: fish_yy    时间: 2010-12-21 05:07
double atof(const char *string); /* Explicit declaration */


vuser_init() {

     float x;
     char *s = "7.2339 by these hilts or I am a villian else";

     x = atof(s);
     /* The %.2f formatting string limits the output to 2 decimal places */
     lr_output_message("%.2f", x);

     return 0;
}

atof函数在应用时要事先声明!
作者: jut984907    时间: 2010-12-21 08:44
欢迎高手赐教,谢谢。
作者: andyguo    时间: 2010-12-21 09:17
float x;
      char *s = "7.2339 by these hilts or I am a villain else";

     double atof(const char *string);

      x = atof(s);
     /* The %.2f formatting string limits the output to 2 decimal places */
     lr_output_message("转换后的数字是%.2f", x);


这样写就没问题了,可以试试
作者: jut984907    时间: 2010-12-21 09:39
andyguo ,高手啊!!
嘿嘿,是个什么原理呢?
作者: andyguo    时间: 2010-12-21 18:15
double atof(const char *string);是将字符串转换为浮点类型




欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2