如何检测strtol()是否未转换数字?我在下面的简单案例中对它进行了测试并输出了0.现在显而易见的问题是如何区分非转换和0的转换?
long int li1;
li1 = strtol("some string with no numbers",NULL,10);
printf("li1: %ld\n",li1);
****
li1: 0
Run Code Online (Sandbox Code Playgroud)
Dav*_*ica 35
该strtol在声明stdio.h如下:
long int strtol(const char *nptr, char **endptr, int base);
Run Code Online (Sandbox Code Playgroud)
strtol提供了一个强大的错误检查和验证方案,允许您确定返回值是否为valid或invalid.从本质上讲,您可以使用3种主要工具.(1)返回的值,(2)的值errno由呼叫设定为,和(3)的地址和内容nptr以及endptr提供给,并且通过设定strtol.(man 3 strtol有关完整的详细信息,请参阅man页面中的示例还提供了一组较短的条件,但下面已对它们进行了扩展以进行说明).
在您的情况下,您询问0返回值并确定它是否有效.正如你所看到的那样,0通过返回的值strtol并不能意味着阅读次数是0或者0是有效的.要确定是否0有效,还必须查看errno调用期间设置的值(如果已设置).具体来说,如果errno != 0和返回的值strtol是0,那么返回的值strtol是INVALID.(该条件将或者表示invalid base,underflow或overflow用errno等于或者EINVAL或ERANGE).
还有第二个条件可能导致strtol返回INVALID 0.在输入中没有读取数字的情况.发生这种情况时,strtol设置值endptr == nptr.因此,在结束0输入值之前,还必须检查指针值是否相等.(可以在字符串中输入多个VALID )00's
以下提供了在评估strtol几种不同测试条件的返回时要检查的不同错误条件的简要示例:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <limits.h>
int main (int argc, char **argv)
{
if (argc < 2) {
fprintf (stderr, "\n Error: insufficient input. Usage: %s int [int (base)]\n\n", argv[0]);
return 1;
}
const char *nptr = argv[1]; /* string to read */
char *endptr = NULL; /* pointer to additional chars */
int base = (argc > 2) ? atoi (argv[2]) : 10; /* numeric base (default 10) */
long number = 0; /* variable holding return */
/* reset errno to 0 before call */
errno = 0;
/* call to strtol assigning return to number */
number = strtol (nptr, &endptr, base );
/* output original string of characters considered */
printf ("\n string : %s\n base : %d\n endptr : %s\n\n", nptr, base, endptr);
/* test return to number and errno values */
if (nptr == endptr)
printf (" number : %lu invalid (no digits found, 0 returned)\n", number);
else if (errno == ERANGE && number == LONG_MIN)
printf (" number : %lu invalid (underflow occurred)\n", number);
else if (errno == ERANGE && number == LONG_MAX)
printf (" number : %lu invalid (overflow occurred)\n", number);
else if (errno == EINVAL) /* not in all c99 implementations - gcc OK */
printf (" number : %lu invalid (base contains unsupported value)\n", number);
else if (errno != 0 && number == 0)
printf (" number : %lu invalid (unspecified error occurred)\n", number);
else if (errno == 0 && nptr && !*endptr)
printf (" number : %lu valid (and represents all characters read)\n", number);
else if (errno == 0 && nptr && *endptr != 0)
printf (" number : %lu valid (but additional characters remain)\n", number);
printf ("\n");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
$ ./bin/s2lv 578231
string : 578231
base : 10
endptr :
number : 578231 valid (and represents all characters read)
$ ./bin/s2lv 578231_w_additional_chars
string : 578231_w_additional_chars
base : 10
endptr : _w_additional_chars
number : 578231 valid (but additional characters remain)
$ ./bin/s2lv 578some2more3stuff1
string : 578some2more3stuff1
base : 10
endptr : some2more3stuff1
number : 578 valid (but additional characters remain)
$ ./bin/s2lv 00000000000000000
string : 00000000000000000
base : 10
endptr :
number : 0 valid (and represents all characters read)
$ ./bin/s2lv stuff578231
string : stuff578231
base : 10
endptr : stuff578231
number : 0 invalid (no digits found, 0 returned)
$ ./bin/s2lv 00000000000000000 -2
string : 00000000000000000
base : -2
endptr : (null)
number : 0 invalid (base contains unsupported value)
Run Code Online (Sandbox Code Playgroud)