While parsing input files, you'll almost never end up with solution that directly uses original stream to fill your variables with values. Format of input might differ, errors might occur... Better approach in this case would be reading the input line by line and processing each line separately. While processing each line you can construct a temporary istringstream
that you can use to read words from it and check whether the word is convert-able to number or not (if 0
is not valid value, use std::atoi
):
std::string line;
while (std::getline(infile,line))
{
if (line.empty()) continue;
std::istringstream is(line);
std::string word;
while (is >> word)
{
int val = std::atoi(word);
if (val)
{
// TODO: number
}
else
{
// word
}
}
}
Alternatively you might consider using std::isdigit
to just check whether first character of word is digit or not:
if (std::isdigit(word[0])) ...