如何读取字符串直到两个连续的空格?

时间:2015-06-22 08:29:00

标签: c format scanf c-strings

scanf()函数的一个众所周知的功能是您可以根据此格式传递格式以扫描输入。

就我而言,我似乎找不到搜索thisthis文档的解决方案。

我有一个字符串(sInput)如下:

#something VAR1 this is a constant string     //some comment

其中VAR1是常量字符串this is a constant的名称。

现在我正在扫描这个字符串:

if(sscanf(sInput, "%*s %s %s", paramname, constantvalue) != 2)
   //do something

当然,当我输出paramnameconstantvalue时,我得到:

VAR1
this

但我希望constantvalue包含字符串,直到找到两个连续的空格(因此它将包含this is a constant string部分。)

所以我试过了:

sscanf(sInput, "%*s %s %[^(  )]s", paramname, constantvalue)
sscanf(sInput, "%*s %s %[^  ]s", paramname, constantvalue)

但没有运气。有没有办法通过sscanf()实现我的目标?或者我应该实现另一种存储字符串的方法吗?

1 个答案:

答案 0 :(得分:3)

scanf family of functions适用于简单的解析,但不适用于您似乎做的更复杂的事情。

可能可能通过使用例如来解决它strstr找到评论启动器"//",在那里终止字符串,然后删除尾随space

相关问题