首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

sscanf()函数提取字符串有关问题

2012-02-21 
sscanf()函数提取字符串问题~#includestdio.hint main(){float fNochar strNo[100]char *str No1.

sscanf()函数提取字符串问题~
#include<stdio.h>

int main()
{
float fNo;
char strNo[100];

char *str = "No=1.1| // No是号码";

sscanf(str, "%[^=]s %[^|]f", &strNo, &fNo );
printf("%f", fNo);
}

我是想把1.1从字符串中提取出来,但调用sscanf()后fNo没被赋值,上网看了些资料,%[^=]s会在strNo后加"\0"结尾,不知这有没有影响?
还想请教以下还有没有其它更好办法提取1.1出来?

[解决办法]
#include <stdio.h> 
#include <stdlib.h>

int main() 

float fNo; 

char *str = "No=1.1 | // No是号码"; 

sscanf(str, "%*[^=]=%f[^|]" ,&fNo ); 
printf("%f", fNo);
system("pause"); 
}
[解决办法]
字符串本来就是以NUL结尾的。没有影响。
对于提取字符串你也可以用strstr进行匹配查找。
用[1.1]直接提取

热点排行