Read Matrix From a file

Hi guys,how do i code my stuff in order to read the following data into array?

Assume in a text file.

content is:
<matrix>
rows = 2
cols = 2

1 2
2 4
</matrix>


my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include	<iostream>
#include	<fstream>

using namespace std;

int main()
{
	fstream inFile;
	int a, b;
	int ar[2][2];
	inFile.open("matrix1.txt");
	{
		if (!inFile)
			cout << "File not found!" << endl;
		else
		for (a = 0; a < 2; a++)
		{
			for (b = 0; b < 2; b++)
			{
				inFile >> ar[a][b];
				cout << "Matrix is:" << ar[a][b] << endl;
			}
		}
	}

	system("pause");
	return 0;
}


following my code above..im still unable to read the matrix in the following file ;-(..can someone help me.

thx in advance
Last edited on
Are you getting error messages?
Just to cover all the bases: is the txt file even being detected? Perhaps it is in the wrong folder. Is it being detected but not opened? Are there any messages being output that might help you track down the problem?

A couple things come to mind:
I don't know if this is a problem, but when I open a text file, I usually use something like the following (for your line 11):

ifstream in("abc.txt", ios::in);

Also, why the braces on lines 12 and 24? They don't seem to serve a purpose.
You have to read the header information before you can read the contents of the matrix. Nowhere do you read the following lines:

<matrix>
rows = 2
cols = 2
Last edited on

Matrix is:-858993460
Matrix is:-858993460
Matrix is:-858993460
Matrix is:-858993460
Press any key to continue . . .


it able to read the file i guess...else it would cout..error opening file...

but getting above error.

Hi doug4,can u elaborate further on the header part..i dun understand ><

thx
If your file contains
<matrix>
rows = 2
cols = 2

1 2
2 4
</matrix>

then you need to write code that deals with the
<matrix>
rows = 2
cols = 2

part, because that's the first thing that's going to get read when you read the file.

Aside from that, on line 11, you never declared inFile, and you don't need the curly braces on lines 12 and 24.
Also, on line 20,
inFile >> ar[a][b] >> endl;
is an error since endl is only for output streams.

You also don't need to #include <cstring> , and you can just declare a and b in the for loop.

This code works for me with your given text file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
//#include <cstring> // Not needed
#include <fstream>

using namespace std;

int main()
{
    int ar[2][2];
    ifstream inFile("abc.txt");
    for (int i = 0; i < 4; ++i)
        inFile.ignore(1000, '\n'); // Eh, whatever (don't actually do this)
    if (!inFile)
        cout << "File not found!" << endl;
    else
        for (int a = 0; a < 2; a++)
        {
            for (int b = 0; b < 2; b++)
            {
                inFile >> ar[a][b];
                cout << "Matrix is:" << ar[a][b] << endl;
            }
        }

    //system("pause");
    return 0;
}
Matrix is:1
Matrix is:2
Matrix is:2
Matrix is:4

But if you're thinking of copy-pasting it, don't, because all I did was make it throw out the first four lines of the text file, which is obviously not what you want since those lines contain valuable information about the size of the matrix.
Last edited on
thx for the reply..

i will edit my code..which has been modified but i din update on top..

May i know what does the code on line 11 & 12 do?

why izit the for loop;for (int i = 0; i < 4; ++i) is 4 instead of 2?
Last edited on
You should read in the amount of rows and cols first, this way you can have dynamic matrices.

#matrix.txt#
matrix:
rows: 5
cols: 3
0 1 9 15 4
5 4 8 1 5
99 5 4 8 9
matrix:
rows: 2
cols: 1
5 9
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int rows = 0;
int cols = 0;
int **matrix;
//Input rows and cols here
...
//Dynamically allocate 2D array
matrix = new int*[ rows ];
for(int i = 0; i < rows; ++i) 
{
    matrix[i] = new int[ cols ];
}
//start getting the matrix from file with for loops
for( int i = 0; i < rows; i++ )
{
    for( int j = 0; j < cols; j++ )
    {
         myFile >> matrix[ rows ][ cols ];
    }
}
Last edited on
May i know what does the code on line 11 & 12 do?

why izit the for loop;for (int i = 0; i < 4; ++i) is 4 instead of 2?

It's what I used to throw out the first four lines of the text file (hence the 4).

In reality, that's not what you want to do -- you'll actually want to read in the number of rows and columns from those lines and adjust the size of your array accordingly. (I just did it to reassure you that your actual matrix inputting code works correctly, but you just forgot to deal with those first few lines)
thank you for all the assistance..appreciate it !!;-)

this is juz my chapter 1...have another 9 chapters to go..can't imagine how it gets more complex..haha
Topic archived. No new replies allowed.