;)

Can you tell me an efficient way of writing and reading a file from two dimensional matrix?
You mean writing and reading a two dimensional matrix from a file? ofstream, ifstream, array/vector and for loops.
Here is some code I used in a Grid class I had for a game. It reads integers from a file and stores them in a 10 x 10 2D array. (Technically it's a vector inside a vector but it's the same syntax)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
bool Grid::LoadGrid(string filename) {
	int input;	
	ifstream fin;
	
	fin.open(filename.c_str());
	
	if (!fin)
		return false;   // file corrupted or missing

	for (int i = 0; i < GRID_MAX and fin; i++) 
		for (int j = 0; j < GRID_MAX and fin; j++) {
			fin >> input;
			pos[j][i] = input;
		}
	 
	return true;
}	



Fixed to use the conventional loop indexes. ; )
Last edited on
@IceThatJaw: Just a little note, the loop convention is actually i, j, k, l, and not i, ii, iii, iv

@OP: bbgst is right. Make an attempt, and if you get stuck post the code you currently have. ;)
Topic archived. No new replies allowed.