user929304
user929304

Reputation: 485

Reading lines from file with different formats c++

I am trying to read an input file line by line and using sscanf to check and extract it. But the lines, although they always have two terms per each, they can be differently formatted, e.g. the file may look like:

valueone 0
valuetwo 55
valuethree version

Had it been only formats of string followed by number, I could always scan as follows: (int test;)

test = sscanf(line, "%s%f", title, &num)

but as shown above, sometimes the format of a line is %s%s (e.g. 3rd line sscanf(line, "%s%s", title, strvalue)), so how can I write the arguments of sscanf to allow for both kinds of line formats?

Upvotes: 0

Views: 146

Answers (1)

Quimby
Quimby

Reputation: 19223

so how can I write the arguments of sscanf to allow for both kinds of line formats?

You can't, sscanf simply cannot do that. What you can do is always use %s %s and then try to parse the second string as double using strtod or strtof.

Upvotes: 1

Related Questions