带字符分隔符的c sscanf [英] c sscanf with character delimiter

查看:85
本文介绍了带字符分隔符的c sscanf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的输入字符串格式为

char *s = "one.two three"

,我想将其分为3个字符串变量. 我在做

and I want to separate it into 3 string variables. I'm doing

sscanf(s, "%s.%s %s", one, two, three);

但是它正在读取"one.two"作为变量一的字符串.我该如何处理."和带有sscanf的空格?

but it's reading in "one.two" as the string for variable one. How do I handle the "." and the whitespace with sscanf?

推荐答案

%s说明符仅在空格处停止.尝试类似的东西:

The %s specifier only stops for a space. Try something like:

sscanf(s, "%[^.].%s %s", one, two, three);


有趣的是,为"%s.%s %s"生成可接受的输入很可能是不可能的,因为%s仅在空格处停止,而.必须立即跟随它.


Interestingly, it's likely impossible to produce an acceptable input for "%s.%s %s" since %s only stops for a space yet a . must immediately follow it.

这篇关于带字符分隔符的c sscanf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆