What should I use instead of sscanf?

Ben Hymers picture Ben Hymers · Jun 23, 2009 · Viewed 29.2k times · Source

I have a problem that sscanf solves (extracting things from a string). I don't like sscanf though since it's not type-safe and is old and horrible. I want to be clever and use some more modern parts of the C++ standard library. What should I use instead?

Answer

Fred Larson picture Fred Larson · Jun 23, 2009

Try std::stringstream:

#include <sstream>

...

std::stringstream s("123 456 789");
int a, b, c;
s >> a >> b >> c;