i would like to C++-read a file line by line, and split each line w.r.t to "\t" character so as to fill in a matrix. my code would go like this
ifstream data_x;
double** test_data = new double*[100];
for(int j = 0 ; j < ; j++)
test_data[j] = new double[4];
data_x.open("X.txt");
int i = 0;
if (data_x.is_open())
{
while (!data_x.eof())
{
char** split = data_x.split("\t")
for(int k = 1 ; k < 4 ; k++)
test_data[i][k];
i++;
}
}
data_x.close();
ifstream data_y;
data_y.open("Y.txt");
i = 0;
if (data_y.is_open())
{
while (!data_y.eof())
{
data_y >> test_data[i][0];
i++;
}
}
data_y.close();
where syntax
char** split = data_x.split("\t")
for(int k = 1 ; k < 4 ; k++)
test_data[i][k];
is approximative. how to make this properly with C++ ?
thanks
Assuming that your file only contains numbers, here’s the standard C++ idiom:
If you know the size of the matrix, you can add the relevant
reservecalls to avoid vector reallocations. You can also add tests for whether there was any unrecognized data on a line, but for now this should get you started.