如何克服SSCANF中的缺点 [英] how to overcome cons in SSCANF
问题描述
如何用sscanf解析一个空的子字符串…….
如果我的字符串str [] =是第一,第三,最后.如果我的第二个子字符串为零,我将无法读取第三个也是最后一个子字符串.是否有解决此错误的解决方案..... ?????
代码:
Hi, how to parse a empty substring with sscanf.......
if my string str[]="is first,,third,final." i cannot able to read the third and final substring if my second substring is zero. Is there an solution to overcome this error.....?????
Code:
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
char *tokenstring = "first,second,25.5,15";
int result, i;
double fp;
char o[10], f[10], s[10], t[10];
void main()
{
result = sscanf(tokenstring,"%[^','],%[^','],%[^','],%s", o,t,s,f);
fp = atof(s);
i = atoi(f);
printf("%s\n %lf\n %s\n %d\n", o,t,fp,i);
}
对于这个程序,如果我执行我的输出是
for this program if i execute my output is
>first
>second
>25.5
>15
但是,如果我尝试删除令牌字符串指针中的子字符串"second" ...并且如果我尝试执行,则输出为:
but if i try to remove the substring "second" in the tokenstring pointer... and if i try to execute, then i get output as:
char *tokenstring = "first,,25.5,15";
>first
>(blank)
>0.00
>0
如何阅读字符串"tokenstring"中的第三和第四子字符串...任何建议??????????
how do i read the third and fourth substring in the string"tokenstring"...any suggestions ??????????
推荐答案
sscanf
功能(例如,参见此处) [竞争对手" [ ^ ]).
sscanf
function (see, for instance here[^]) does NOT take regular expressions in the format string.
You may either:
- Use a library for regular expression parsing (see, for instance, ''The Competitors''[^]).
- Parse yourself the input string (you may wisely use,
strtok
for the purpose, see strtok documentation[^]).
这篇关于如何克服SSCANF中的缺点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!