I have the output of another program, which is more designed to be read on a person than machine-readable, but I'm still going to parse it. This is not too complicated.
However, I'm wondering what is the best way to do this in C ++. It is rather a general practice question.
I looked into Boost.Spirit and even worked a little. This is madness! If I were developing a language that I read, this could be the right tool for the job. But since this is so, given its extreme compilation times, a few error pages from g ++, when I do something wrong, it's just not what I need. (I also lack performance at runtime.)
Thinking about using the C ++ operator <<, but that seems futile. If my file has lines such as “John has 5 widgets” and others “Mary works on Ramsay Street 459”, how can I make sure that I have a line of the first type in my program and not the second type? I have to read the whole string and then use things like string::find and string::substr . I suppose.
And that leaves sscanf . He would do well with the above cases.
if( sscanf( str, "%s has %d widgets", chararr, & intvar ) == 2 ) // then I know I matched "foo has bar" type of string, // and I now have the parameters too
So I'm just wondering if I'm missing something or if C ++ really doesn't have a built-in alternative.
c ++ string parsing
Scott
source share