0

This is the sample text file content:

5 //columns

Id,Age,history,chemistry,biology //column names

100// number of data rows

3245167,12,45,78,12 //data rows separated by commas

30980424,10,26,38,98

and so on..

This is the general code i have so far:

int main()
{
  //prints out input from file.

  ifstream myFile;
  myFile.open("WRITE FILE NAME");

  while(Myfile.good()) { // good means while there is smthg in the file keep reading it
    // until you reach to the end.

    string line;

    getline(myFile, line, ','); //read text until comma, then stop and store in variable.
    cout << line << endl;
  }
  return 0;
}
Beta
  • 96,650
  • 16
  • 149
  • 150
mooleless
  • 1
  • 3
  • 1
    You have solved half of the problem: parsing the file. Do you know how to put values in a 2D array? Or a 1D array? – Beta Sep 24 '20 at 03:09
  • Instead of using good as the loop condition, use the getline command as the loop condition. – Jerry Jeremiah Sep 24 '20 at 03:29
  • Possible duplicate: https://stackoverflow.com/questions/34218040/how-to-read-a-csv-file-data-into-an-array https://stackoverflow.com/questions/18777267/c-program-for-reading-csv-writing-into-array-then-manipulating-and-printing-i https://stackoverflow.com/questions/53148332/c-reading-csv-file-and-assigning-values-to-array https://stackoverflow.com/questions/48994605/csv-data-into-a-2d-array-of-integers – Jerry Jeremiah Sep 24 '20 at 03:32

1 Answers1

0

You have a general outline of parsing the file itself, the data will be read from the file left to right. So there's two things I'd recommend for you to do at this point since you've already parsed the data. You'll probably want something to hold it like a queue to store and hold all the data and a double for loop to place it into the 2D array so like this:

std::queue<string> holder;
std::string myArray[row][col];

getline(myFile, line, ',');
holder.push(line);

for(int i=0; i < row; i++)
{ 
    for(int j=0; j < col; j++)
    {
      myArray[i][j] = holder.pop();
    }
 }
Life_57
  • 1
  • 2
  • Also I apologize if anything looks out of place, first time helping someone on this website, but this should work for your case though! :) – Life_57 Sep 24 '20 at 03:43
  • ahh no worries it is helpful for me really appreciate it and its the first time me asking qs in this website too..i – mooleless Sep 24 '20 at 03:57