Hi there! I have a question related to filing in c++. I have to read data from a text file and load it into 2-d dynamic array. Here is the problem statement:
5 3
3 8
1 4 2
5 7 2
4 6 8
0 9 3
1 2 8
6 4 2 8 0 5 2 7
7 3 8 4 2 8 0 5
3 1 6 7 9 5 3 1
First row gives the dimensions (row, column) of iMatrixA, second row give the dimensions of iMatrixB. From third row onwards the data of iMatrixA is given (row wise) followed by data of iMatrixB.
i have read the rows and columns in the first two lines of this text file. Also i have read the first array which is of the order 5x3. Now i am facing a problem in reading the second array which is of the order 3x8. Here is my code:
#include <iostream>
#include <fstream>
using namespace std;
void main()
{
ifstream inFile;
int iRows1;
int iCols1;
int iRows2;
int iCols2;
inFile.open("matrixMul.txt");
if (!inFile)
{
cout << " unable to open file" << endl;
}
else
{
inFile >> iRows1 >> iCols1 ;
inFile >> iRows2 >> iCols2;
int** iMatrix1 = new int*[iRows1];
for (int i=0; i<iRows1;i++)
{
iMatrix1[i] = new int[iCols1];
}
int** iMatrix2 = new int*[iRows2];
for (int j=0; j<iRows2; j++)
{
iMatrix2[j]= new int[iCols2];
}
for (int i=0; i<iRows1; i++)
{
for (int j=0; j<iCols1; j++)
{
inFile >> iMatrix1[i][j];
}
}
for (int i=0; i<iRows1; i++)
{
for (int j=0; j<iCols1; j++)
{
cout << iMatrix1[i][j] << " " ;
}
cout << endl;
}
}
system("pause");
}