字符串转换为数字的问题(strtod)

[英]Problem with string conversion to number ( strtod )


I am using strtod( ) function to extract an environment variable as a string, and then changing it to double using strtod:

我使用strtod()函数将环境变量提取为字符串,然后使用strtod将其更改为double:

enter code here
 char strEnv[32];
 strncpy(strEnv, getenv("LT_LEAK_START"), 31);
 // How to make sure before parsing that env LT_LEAK_START is indeed a number?
 double d = strtod(strEnv, NULL);

Now i want to make sure that this number entered by user is a number and not a string or special character. How can i make sure of that?

现在我想确保用户输入的这个数字是一个数字而不是字符串或特殊字符。我怎样才能确定?

A code snippet would be of great help.

代码片段会有很大帮助。

Thanks in advance.

提前致谢。

6 个解决方案

#1


14  

The 2nd argument to the strtod function is useful.

strtod函数的第二个参数很有用。

char *err;
d = strtod(userinput, &err);
if (*err == 0) { /* very probably ok */ }
if (!isspace((unsigned char)*err)) { /* error */ }

Edit: examples added

编辑:添加了示例

The strtod function tries to convert the initial portion of the 1st argument to a double and stops either when there are no more chars, or there is a char that can't be used to make a double.

strtod函数尝试将第一个参数的初始部分转换为double,并在没有更多字符时停止,或者存在不能用于生成double的char。

input         result
----------    ----------------------------
"42foo"       will return 42
              and leave err pointing to the "foo" (*err == 'f')

"     4.5"    will return 4.5
              and leave err pointing to the empty string (*err == 0)

"42         " will return 42
              and leave `err` pointing to the spaces (*err == ' ')

#2


2  

man strtod: If no conversion is performed, zero is returned and the value of nptr is stored in the location referenced by endptr.

man strtod:如果没有执行转换,则返回零,并将nptr的值存储在endptr引用的位置。

char * endptr;
double d = strtod(strEnv, &endptr);
if (strEnv == endptr)
   /* invalid number */
else
   ...

#3


2  

Surely you could do worse than just reading the man page for strtod() and acting upon that. E.g. on my Linux system it says:

当然,你可以做的不仅仅是阅读strtod()的手册页并采取行动。例如。在我的Linux系统上它说:

RETURN VALUE
       These functions return the converted value, if any.

       If  endptr  is  not  NULL,  a pointer to the character after the last character used in the conversion is stored in the location referenced by
       endptr.

       If no conversion is performed, zero is returned and the value of nptr is stored in the location referenced by endptr.

       If the correct value would cause overflow, plus or minus HUGE_VAL (HUGE_VALF, HUGE_VALL) is returned (according to the sign of the value), and
       ERANGE is stored in errno.  If the correct value would cause underflow, zero is returned and ERANGE is stored in errno.

That pretty much tells you what you need to do in order to handle errors. Also, like Johann Gerell said, you also need to check whether getenv() succeeded; a similar approach works there, i.e. check the man page and write error handling code according to that.

这几乎告诉你为了处理错误你需要做什么。另外,就像Johann Gerell所说,你还需要检查getenv()是否成功;类似的方法在那里工作,即检查手册页并根据它编写错误处理代码。

#4


1  

  • First, check the return value of getenv - if it's NULL, then that environment variable doesn't exist.
  • 首先,检查getenv的返回值 - 如果它为NULL,则该环境变量不存在。

  • Second, if the return value of getenv isn't NULL, then you have the value, as a string.
  • 其次,如果getenv的返回值不为NULL,那么您将该值作为字符串。

  • Third, don't set the char ** endptr parameter of strtod to NULL, but use it to check the validity of the converted value, also check for 0.0.
  • 第三,不要将strtod的char ** endptr参数设置为NULL,而是使用它来检查转换值的有效性,同时检查0.0。

#5


0  

That second argument to strtod, which you've set to NULL, can be a pointer-to-pointer-to-char; the pointer-to-char that it points to will get set to the character after the last thing strtod managed to parse. If that's the end of the string, or at least there's nothing after it but whitespace, then what you had was a number. Otherwise, it was something else.

strtod的第二个参数,你设置为NULL,可以是一个指向char的指针;它指向的指向char的指针将在strtod设法解析的最后一个东西之后被设置为该字符。如果这是字符串的结尾,或者至少在它之后没有什么,只有空格,那么你所拥有的是一个数字。否则,它是另一回事。

#6


0  

I don't know much about this language but I do know that strtod() will return 0.0 if the input is wrong. Maybe you could use a regular expression to validate the input string is a number.

我对这种语言知之甚少,但我知道如果输入错误,strtod()将返回0.0。也许您可以使用正则表达式来验证输入字符串是否为数字。

智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2011/04/07/3a67a19f918df1c8c34875180bbaaa56.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告