How to read a line up to two consecutive spaces?
A well-known feature of the functions scanf()
is that you can transfer the format to scan the input according to that format.
In my case, I cannot find a solution looking for this and this documentation.
I have a string ( sInput
) like the following:
#something VAR1 this is a constant string //some comment
Where the VAR1
name of the constant string is implied this is a constant
.
Now I am looking at this line like this:
if(sscanf(sInput, "%*s %s %s", paramname, constantvalue) != 2)
//do something
And of course, when I output paramname
and constantvalue
, I get:
VAR1
this
But I would like it to constantvalue
contain the string until two consecutive spaces are found (so it will contain the part this is a constant string
).
So I tried:
sscanf(sInput, "%*s %s %[^( )]s", paramname, constantvalue)
sscanf(sInput, "%*s %s %[^ ]s", paramname, constantvalue)
But no luck. Is there a way to achieve my goal using sscanf()
? Or do I need to implement another way to store the string?
source to share
scanf
the family of functions is useful for simple analysis, but not for more complex things like you.
Maybe you can possibly solve it by using, for example, strstr
to find the starter comment "//"
, end the line there, and then remove the trailing space .
source to share