moffeltje
moffeltje

Reputation: 4658

How to read string until two consecutive spaces?

A well known function of the scanf() functions is that you can pass a format to scan input according to this format.

For my case, I cannot seem to find a solution searching this and this documentation.

I have a string (sInput) as the following:

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

Where VAR1 is meant to be the name of the constant string this is a constant.

Now I am scanning this string 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 to have constantvalue to contain the string until two consecutive spaces are found (so it would contain the part this is a constant string).

So therefore I tried:

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

But without luck. Is there a way to achieve my goal with sscanf()? Or should I implement another way of storing the string?

Upvotes: 3

Views: 152

Answers (1)

Some programmer dude
Some programmer dude

Reputation: 409356

The scanf family of functions are good for simple parsing, but not for more complicated things like you seem to do.

You could probably solve it by using e.g. strstr to find the comment starter "//", terminate the string there, and then remove trailing space.

Upvotes: 3

Related Questions