Quick Simple Parser (scanf)

I tend to fall back on scanf when I have to parse simple text files. - Simple string parsing with C++

char line[SOME_SIZE];
while (fgets(line, SOME_SIZE, file)) {
    int x, y, z;
    if (3 == sscanf(line, "foo: [%d %d %d]", &x, &y, &z)) {
        continue;
    }
    float w;
    if (1 == sscanf(line, "baz: %f", &w)) {
        continue;
    }
}

see also:

Written on December 18, 2022, Last update on June 23, 2024
c++ config parser single-header json toml