how to read a comma delimited file into an array c 2b 2b

Solutions on MaxInterview for how to read a comma delimited file into an array c 2b 2b by the best coders in the world

showing results for - "how to read a comma delimited file into an array c 2b 2b"
Jonas
27 Jul 2018
1#include <fstream>
2#include <sstream>
3#include <iostream>
4#include <vector>
5
6int main()
7{
8    std::ifstream inFile("registration.txt");
9    if (inFile.is_open())
10    {
11        std::string line;
12        while( std::getline(inFile,line) )
13        {
14            std::stringstream ss(line);
15
16            std::string ID, fname, lname;
17            std::getline(ss,ID,',');    std::cout<<"\""<<ID<<"\"";
18            std::getline(ss,fname,','); std::cout<<", \""<<fname<<"\"";
19            std::getline(ss,lname,','); std::cout<<", \""<<lname<<"\"";
20
21            std::vector<std::string> enrolled;
22            std::string course;
23            while( std::getline(ss,course,',') )
24            {
25                 enrolled.push_back(course); std::cout<<", \""<<course<<"\"";
26            }
27            std::cout<<"\n";
28        }
29    }
30    return 0;
31}