sscanf 字符串拆分不起作用 [英] sscanf string splitting not working
本文介绍了sscanf 字符串拆分不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
谁能解释一下为什么下面的代码中没有拆分字符串
Can someone please explaing why the string is not splitted in the following code
#include <stdio.h>
int main(void)
{
char name[] = "first:last";
char first[20], last[20];
sscanf(name, "%s:%s", first, last);
printf("first: %s, last: %s", first, last);
return 0;
}
输出为
第一个:第一个:最后一个,最后一个:
first: first:last, last:
但应该是
第一个:第一个,最后一个:最后一个
first: first, last: last
请在此处检查代码http://ideone.com/JDSTt
推荐答案
你可以这样使用:
sscanf(name, "%[^:]:%s", first, last);
:
不是空格,因此常规 %s
不会将其视为分隔符.有关详细信息,请参阅 scanf
.
:
is not whitespace, so a regular %s
will not consider it as a delimiter. See scanf
for more details.
(编辑演示:http://ideone.com/m4LVP)
这篇关于sscanf 字符串拆分不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文