2

I was wondering if there was a way to read all of the "words" from a line of text.

the line will look like this: R,4567890,Dwyer,Barb,CSCE 423,CSCE 486

Is there a way to use the comma as a delimiter to parse this line into an array or something?

cskwrd
  • 2,803
  • 8
  • 38
  • 51
  • Duplicate of: http://stackoverflow.com/questions/1474790/how-to-read-write-into-from-text-file-with-comma-separated-values/1474819 – Martin York Sep 27 '09 at 18:25

2 Answers2

12

Yes, use std::getline and stringstreams.

std::string str = "R,4567890,Dwyer,Barb,CSCE 423,CSCE 486";

std::istringstream iss(str);
std::vector<std::string> words;

while (std::getline(iss, str, ','))
  words.push_back(str);
hrnt
  • 9,882
  • 2
  • 31
  • 38
0
//#include sstream with angular braces in header files 

std::string str = "R,4567890,Dwyer,Barb,CSCE 423,CSCE 486";

std::istringstream iss(str,istringstream:in);

vector<std::string> words;

while (std::getline(iss, str, ','))
  words.push_back(str);
takrl
  • 6,356
  • 3
  • 60
  • 69
Ravindra
  • 9
  • 1