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");
}

Recommended Answers

All 3 Replies

You dont seem to have a 2d array here my friend.
2d array is only

col and row. once you name int cols1 and int cols2 then int rows1 and int rows2.
You got 4d array.

Understand that 2d arrays are rows and cols. no more no less so get back to arrays 101 again ok?

You can phrase this as a 1D array,

    array[n] == array[ width * y + x ]

Hi,

I would suggest making a function for this task, its job would be to do the n*n read from the file. The function would prototype something like int read_matrix_ints_from_file( ifstream& fstream, int** outputMatrix, size_t rows, size_t cols );

Notes:
- size_t is just an unsigned integer type that can hold the size of any memory your compiler can address, so on 64 bit systems it can hold a much larger number than on 32 bit systems
- The integer return could be a boolean, its purpose is just to indicate if the function succeeded, reached eof before reading all data or whatever.

The benefit here is saving on code duplication and if you fix a bug in reading the first matrix you dont have to copy and paste the code to fix the second one. Some might say its a bit overkill for what you are doing but i would always say refactoring nontrivial common code into functions is always a good thing.

Then finally your core code is quite neat

...
    else
    {
        inFile >> iRows1 >> iCols1 ;
        inFile >> iRows2 >> iCols2;

        int success;
        int** matrix1 = new int[iRows1][iCols1];
        int** matrix2 = new int[iRows2][iCols2];

        success = read_matrix_ints_from_file( inFile, matrix1, iRows1, iCols1 );
        /* check for errors */
        success = read_matrix_ints_from_file( inFile, matrix2, iRows2, iCols2 ); 
        /* check for errors */

        /* do whatever you need to do next */
    }

    /* program cleanup */

    ...
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.