C 忽略周围有字母的数字
每当你需要在缓冲区中挑选数值时,你就要考虑C 忽略周围有字母的数字,c,C,每当你需要在缓冲区中挑选数值时,你就要考虑strtoX()(其中X可以是l,ul,f,d,forstrtol(),等等)原因是双重的。(1) 这些函数旨在通过提供指向字符串本身的指针参数,以及更新后指向最后一个数字成功转换后的下一个字符的endptr参数来实现这一点;以及(2)这些功能为转换的各个方面提供完整的错误检测 由于endptr参数被更新为最后一个转换的数字后的一位,您只需在缓冲区中循环,尝试转换,成功转换后,您只需更新指向endptr的初始指针并重复,直到字符用完为止。看 由于endp
strtoX()
(其中X
可以是l
,ul
,f
,d
,forstrtol()
,等等)原因是双重的。(1) 这些函数旨在通过提供指向字符串本身的指针参数,以及更新后指向最后一个数字成功转换后的下一个字符的endptr
参数来实现这一点;以及(2)这些功能为转换的各个方面提供完整的错误检测
由于endptr
参数被更新为最后一个转换的数字后的一位,您只需在缓冲区中循环,尝试转换,成功转换后,您只需更新指向endptr
的初始指针并重复,直到字符用完为止。看
由于endptr
被更新为最后一个转换的数字后的一个点,您只需检查指向endptr
的字符是字符串的nul终止字符还是!isspace(*endptr)
,以确定该组数字中是否包含非数字
您的案例的一个简单实现是:
EXP:
input: 1 2 3 4a desired output-> 1 2 3 my ouput-> 1 2 3 4 4
input: 1 2 3 a4 desired output-> 1 2 3 my ouput-> 1 2 3 3
input: 1 2 3 a b c 4 5 6 7 desired output-> 1 2 3 my output-> 1 2 3 3 3 3 4 5 6 7
Valid input EXP:
input: 12 367 98 -3 -67 desired output-> 12 367 98 -3 -67
示例使用/输出
$ cat dat/numwalpha.txt
1 2 3 4a
1 2 3 a4
1 2 3 a b c 4 5 6 7
添加错误报告
失败时,您只需输出从endptr
(ep
)开始的字符即可查看失败原因。如果愿意,您可以使用DEBUG
define,包括一个预处理器指令来打开/关闭错误报告,例如:
$ ./bin/strtol_numonly dat/numwalpha.txt
1 2 3
1 2 3
1 2 3
它显示了第一个案例由于结尾处的
'a'
而失败,第二个案例由于要转换的字符串为“a4”
而失败,最后一个案例是因为字符串中只剩下“ABC4567”
,而'a'
是下一个转换。欢迎您这样做!所以基本上你想提取字符串中的所有整数并将它们放入整数数组中?我本想将它们放入链表中,但为了简单起见,让我们说数组。一次读取一行是正确的方法,但对于转换,strtol()
是合适的工具,使用endptr
参数确定转换后是否保留任何非数字或非空白字符。您可以使用sscanf()
和%n
从str+count+n
向前扫描来检查非数字/非空白字符。嗯,好的。我将尝试使用strtol()来代替,它看起来是更简单的方法。谢谢
$ cat dat/numwalpha.txt
1 2 3 4a
1 2 3 a4
1 2 3 a b c 4 5 6 7
$ ./bin/strtol_numonly dat/numwalpha.txt
1 2 3
1 2 3
1 2 3
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <errno.h>
#define MAXC 1024
int main (int argc, char **argv) {
char buf[MAXC]; /* buffer to hold each line */
/* use filename provided as 1st argument (stdin by default) */
FILE *fp = argc > 1 ? fopen (argv[1], "r") : stdin;
if (!fp) { /* validate file open for reading */
perror ("file open failed");
return 1;
}
while (fgets (buf, MAXC, fp)) { /* read each line into buf */
char *p = buf, *ep = p; /* pointers to use with strtol() */
for (;;) { /* loop continually */
errno = 0; /* reset errno */
long val = strtol (p, &ep, 0); /* attempt conversion to long */
if (p == ep) { /* check if no characters converted */
#ifdef DEBUG
fprintf (stderr, "error: no digits converted in '%s'.\n", p);
#endif
break;
}
else if (errno) { /* check for under/overflow */
#ifdef DEBUG
fprintf (stderr, "error: over/under-flow in conversion or '%s'.\n", p);
#endif
break;
}
else if (*ep && !isspace(*ep)) { /* check next char for non-space */
#ifdef DEBUG
fprintf (stderr, "error: mixed alphanumeric '%s'.\n", ep);
#endif
break;
}
else
printf (" %ld", val); /* good all-digit conversion */
p = ep; /* update pointer to endpointer */
}
putchar ('\n'); /* tidy up with newline */
}
if (fp != stdin) /* close file if not stdin */
fclose (fp);
}
$ ./bin/strtol_numonly dat/numwalpha.txt
error: mixed alphanumeric 'a
'.
1 2 3
error: no digits converted in ' a4
'.
1 2 3
error: no digits converted in ' a b c 4 5 6 7
'.
1 2 3