【发布时间】:2015-04-21 18:39:04
【问题描述】:
有没有办法使用sscanf() 来计算字符串中浮点数的个数?
count = sscanf(string, " %f %f /* an so on.. */", &temp, %temp2 /* ..*/);
我可以放大量的"%f" 和变量,但这似乎是个愚蠢的想法,有什么办法让它灵活吗?
你能帮帮我吗?
编辑:我试图以这种方式使用strtok(),但它不起作用
substring = strtok(lines_content, " " );
temp = sscanf(substring, "%f", &value);
if(temp == 1)
{
no_of_floats_in_line++;
}
fflush(stdin);
while(token = strtok(NULL, " ") != NULL)
{
substring = strtok(NULL, " ");
temp = sscanf(substring, "%f", &value);
fflush(stdin);
if(temp == 1)
{
no_of_floats_in_line++;
}
}
【问题讨论】:
-
使用带有
strtok()的循环来隔离每个浮点子字符串。 -
“不起作用” - 请具体说明。
标签: c string count floating-point scanf