First use a string tokenizer
std::string text = "token, test 153 67 216"; char_separator<char> sep(", "); tokenizer< char_separator<char> > tokens(text, sep);
Then, if you do not know exactly how many values you will get, you should not use single abc variables, but an array, for example int input[200] , or better, std::vector , which can adapt to the number of elements read.
std::vector<int> values; BOOST_FOREACH (const string& t, tokens) { int value; if (stringstream(t) >> value)
You must:
#include <string> #include <vector> #include <sstream> #include <iostream> //std::cout #include <boost/foreach.hpp> #include <boost/tokenizer.hpp> using boost::tokenizer; using boost::separator;
By the way, if you are programming with C ++, you may need to avoid using printf and prefer std::cout
source share