how to read matrix from a file

here my code, please help me fix:

I think my code is right, but I can't get the right row and column, I write a test, the row and column is always 0 and 1, why?
and I have some problem in the comment, hope you can help me.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class matrix {
public:
	matrix();
	~matrix();								
	bool read(std::ifstream& inFile, std::string filename);
	void print()const;
private:
	int row;
	int column;
	int** m;
private:
	void getColumn(std::ifstream& inFile);
	void getRow(std::ifstream& inFile);
};


implementation:
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
bool matrix::read(std::ifstream& inFile, std::string filename) {
	//std::ifstream inFile;
	inFile.open(filename.c_str());

	getRow(inFile); 
        inFile.close();               // I found if I want to read from the beginning, I have to close the file, do you have a better way?
	inFile.open(filename.c_str());
	getColumn(inFile);          // why can't I get the right row and column, 
                                    // it's always 0 and 1
        inFile.close(); 
        inFile.open(filename.c_str());
        m = new int*[row];
        for(int i = 0; i < row; ++i) {
	   m[i] = new int[column]; 
           for(int j = 0; j < column; ++j) {
		inFile >> m[i][j];
		inFile.get();
	    }
        }
	return true;
}

void matrix::print()const {
	for(int i = 0; i < row; ++i) {
		for(int j = 0; j < column; ++j) {
			std::cout <<  m[i][j];
			if(j == column - 1) {
				std::cout << std::endl;
			} else {
				std::cout << ' ';
			}
		}
	}
}

void matrix::getRow(std::ifstream& inFile)  {
	row = 0;
	std::string temp = "";
	while(std::getline(inFile, temp)) {
		++row;
	}
}

void matrix::getColumn(std::ifstream& inFile) {
	column = 0;
	int temp = 0;
	while(inFile >> temp && inFile.get() != '\n') {
		++column;
	}
	++column;

}



Is my dynamic memory right?
1
2
3
4
5
6
7
8
9
10
11
matrix::matrix(const matrix& M) {
	row = M.row;
	column = M.column;
	m = new int*[row];
	for(int i = 0; i < row; ++i) {
		m[i] = new int[column];
		for(int j = 0; j < column; ++i) {
			m[i][j] = M.m[i][j];
		}
	}
}
Topic archived. No new replies allowed.