So I have a task to read two 2d arrays from a .txt file. The file looks like this:
2 3 4 5 6 7
2 6 8 2 4 4
6 3 3 44 5 1
#
4 2 1 6 8 8
5 3 3 7 9 6
0 7 5 5 4 1
The '#' character is used for defining a boundary between two arrays.
And both of 2d arrays has to be dynamically allocated. This is what I have:
int col = 0, row = 1;
int temp = 0;
char c;
ifstream file("input1.txt");
col = 0;
row = 1;
// calculating the no. of rows and columns of first 2d array in the text file (don't know how to do that for the second but this works perfectly)
do {
file.get(c);
if ((temp != 2) && (c == ' ' || c == '\n'))
col++;
if (c == '\n')
{
temp = 2;
row++;
}
} while (file);
// Used for rewinding file.
file.clear();
file.seekg(0, ios::beg);
// now I am using the info on rows and columns to dynamically allocate 2d array.(this is working fine too)
int** ptr = new int*[row];
for (int i = 0; i < col; i++)
{
ptr[i] = new int[col];
}
// here I am filling up the first 2d array.
for (int i = 0; i < row; i++)
{
for (int k = 0; k < col; k++)
{
file >> ptr[i][k];
cout << ptr[i][k] << ' ';
}
cout << endl;
}
// code above works fine when there is only one matrix in file(without the '#' sign)
// these are used for storing dimensions of second 2d array and for dynamically allocating him
int row_two = 1, col_two = 0;
Now, what should I do next in order to recognize the '#' character and continue reading the next matrix? Next 2d array should be called ptr_two
.
Just to note, I can't use vectors, only 2d arrays.