smatter
smatter

Reputation: 29168

C++: Reading from stringstream

I have an istringstream object with string of format STRING,INT,INT,INT eg. "name,20,30,40" I want to read the values into variables of specific types such as std:string and int. How can I do that?

Upvotes: 1

Views: 1544

Answers (1)

Cubbi
Cubbi

Reputation: 47408

The lazy way:

getline(stream, str, ',');
char c;
stream >> i1 >> c >> i2 >> c >> i3;

It is "lazy" because it does not handle format errors in any sensible way.

The smarter ways would be split on commas into a vector of strings (which can then be converted into integers as needed), or use a full-fledged parser, such as boost.spirit.

Upvotes: 3

Related Questions