问题
I have an input string of the form
char *s = "one.two three"
and I want to separate it into 3 string variables. I'm doing
sscanf(s, "%s.%s %s", one, two, three);
but it's reading in "one.two" as the string for variable one. How do I handle the "." and the whitespace with sscanf?
回答1:
The %s
specifier only stops for a space. Try something like:
sscanf(s, "%[^.].%s %s", one, two, three);
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.
来源:https://stackoverflow.com/questions/12654599/c-sscanf-with-character-delimiter