Hey Everyone! I'm having trouble with a C++ program I'm trying to write. I need to input a text file into a 2D array and sort it into 13 rows, 5 columns using strtok. I need to use strtok because some of the words are separated by ws, others by a comma.
For example,
123 Mike November,12 1990
.
.
.
I'm not sure how to use the strtok when I'm creating the array from the file. Before I thought of strtok, I was making 4 column 2D-arrays by just doing myFile>>_array[j]; (which is what my code shows below).
If anyone could help me out with this I would really appreciate it!! :)
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
const int ROWS = 13;
const int COLS = 4;
string _array[ROWS][COLS];
ifstream myFile("data.txt");
ofstream myFileOut("data2.txt");
for (int i = 0; i < 12; i++){
for(int j = 0; j < 4; j++){
myFile >> _array[i][j];
}
}
for (int i = 0; i < 12; i++){
cout << "\n";
for (int j = 0; j < 4; j++){
cout << _array[i][j] << " ";
myFileOut << _array[i][j] <<endl;
}
}
return 0;
}