How to read a text file with data separated by commas into different variables respectively

9780099584353,Haruki Murakami,Norwegian Wood,Vintage Publishing,2003,1,37.90,1,2

The data represents isbn code, author, title, publisher, year published, quantity, price, rack, level number respectively.

How can I read the text file into my program variables respectively?
Basics may be something along these lines.
1
2
3
4
5
6
7
8
// get each line from the file
while ( getline(inf,line) ) {
  istringstream is(line);
  string s;
  // get each comma field from the line
  while ( getline(is,s,',') ) {
  }
}
oh okok thank you so much
but what if i want to insert the text file into an array? how do i do that?
What do you mean by that? What do you want an array of? Lines? Entire files? Comma-separated items?
> but what if i want to insert the text file into an array? how do i do that?
*sigh*
1
2
3
4
5
6
  vector<string> fields;
  // get each comma field from the line
  while ( getline(is,s,',') ) {
    fields.push_back(s);
  }
  spreadsheet.push_back(fields);

Have a guess what type spreadsheet could possibly be.

Topic archived. No new replies allowed.