I would like to use C++ to read a .CSV file and extract each line and put each element separated by ',' into a 2D array. but there seems to be error in the reading and i cannot find the problem.
My C++ Code:
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main( )
{
std::string line2;
ifstream in2("sample-to-remove.txt");
string entryLine[10][2];
int x = 0;
while (!in2.eof())
{
getline(in2, line2, '\n');
stringstream ss(line2);
std::string token;
int y=0;
while (std::getline(ss, token, ','))
{
std::cout << token << '\t';
entryLine[x][y] = token;
y++;
}
x++;
}
for(int a= 0 ; a < 10 ; a++ )
{
for(int b= 0 ; b < 2 ; b++ )
{
cout << entryLine[a][b] << endl;
}
}
in2.close();
return 0;
}
My CSV File:
9834117,audriwxh@gmail.com
9234049,calinwj@hotmail.com