read from .txt file

i am new to C++ programming, i having problem in a small task of my assessment.

As in question, i having a structure called matrix as shown below to represent a matrix your read from a text file.

struct matrix
{
double mat[30][30]; //content of the matrix mat
int row; //number of row in the matrix mat
int col; ////number of col in the matrix mat
};


Declare 3 variables of structure matrix, m1, m2 and m3 in main to be used as describe below.

Task:

1.Write a function void readFile(char *filename, matrix *m) that will read a matrix from a text file and copy to the structure matrix m.
It is unclear

1. whether the program has to be written in C or C++
2. what is the format of the text file.
1. is in c++ format..
2. the .txt file is something like



<matrix>
	
rows = 2
cols = 2

1 2
2 4
</matrix>


basically this the sample output:



Matrix Operation //Menu list 
(1) Add two matrices
(2) Multiply two matrices
(3) Take transpose of a matrix
(4) Display matrix from variable
(5) Display matrix from text file
(6) Exit


Choice: 1 //User to key in choice
Enter file name for matrix 1: matrix1.txt //User to enter the filename
Enter file name for matrix 2: matrix2.txt //User to enter the filename


Result of Addition:  //Prompt
1 2
2 4
+
1 2
2 4
=
2 4
4 8


sorry in unclear form, i just stuck in the reading from the .txt file and copy to struc matrix. I am weak in Input/output file and array. thanks in advanced.
any solution?
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <fstream> //needed for ofstream

using namespace std;


//Test output program
int main() {

    ofstream out("file.txt"); //output file stream
    out << "Nope, there's\nonly trash here.\n"; //notice it's going to "out", our file

    return 0;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <fstream> //needed for ifstream
#include <string>

using namespace std;


//Test input program
int main() {

    ifstream in("file.txt"); //input file stream
    string str; //holds input
    while( getline(in, str) ) //continue if another line could be read
        cout << str << '\n'; //print that line to standard output

    return 0;
}
Topic archived. No new replies allowed.