First use 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 shouldn't use single variables a b c
, but an array like int input[200]
, or better, a std::vector
, which can adapt to the number of elements you read.
std::vector<int> values;
BOOST_FOREACH (const string& t, tokens) {
int value;
if (stringstream(t) >> value) //return false if conversion does not succeed
values.push_back(value);
}
for (int i = 0; i < values.size(); i++)
std::cout << values[i] << " ";
std::cout << std::endl;
You have to:
#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 C++ you might want to avoid using printf
, and prefer std::cout