我有以下代码:
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
void main(void)
{
int data;
char * tmp;
data = strtol("23ef23",&tmp,10);
printf("%d",errno);
getchar();
}
输出为 0 ...
why?
我正在使用 Visual Studio 2010 C++
代码必须与 C89 兼容。
strtol
仅套errno
对于溢出情况,并不表示解析失败。为此,您必须检查结束指针的值,但您需要存储指向原始字符串的指针:
char const * const str = "blah";
char const * endptr;
int n = strtol(str, &endptr, 0);
if (endptr == str) { /* no conversion was performed */ }
else if (*endptr == '\0') { /* the entire string was converted */ }
else { /* the unconverted rest of the string starts at endptr */ }
我认为唯一需要的错误值是下溢和溢出。
相反,如果整个字符串在转换中已被消耗,则您有*endptr = '\0'
,这可能是您可能需要检查的另一件事。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)