I\'ve been using C++ for quite a long time now but nevertheless I tend to fall back on scanf when I have to parse simple text files. For example given a config
Boost.Spirit is not reserved to parse complicated structure. It is quite good at micro-parsing too, and almost match the compactness of the C + scanf snippet :
#include
#include
#include
using namespace boost::spirit::qi;
int main()
{
std::string text = "foo: [3 4 5]\nbaz: 3.0";
std::istringstream iss(text);
std::string line;
while (std::getline(iss, line))
{
int x, y, z;
if(phrase_parse(line.begin(), line.end(), "foo: [">> int_ >> int_ >> int_ >> "]", space, x, y, z))
continue;
float w;
if(phrase_parse(line.begin(), line.end(), "baz: ">> float_, space , w))
continue;
}
}
(Why they didn't add a "container" version is beyond me, it would be much more convenient if we could just write :
if(phrase_parse(line, "foo: [">> int_ >> int_ >> int_ >> "]", space, x, y, z))
continue;
But it's true that :
So ultimately, for simple parsing I use scanf like everyone else...