How to load the following file into variables and matrix?

I have the following file "maze.in" and I need to input this file such that it assigns values to six variables and load values into matrix (matrix[][]). The text file is:

1
2
3
4
5
6
7
8
9
10
10 7
0 8
5 9
xxxxxxxx x
x   x x  x
x x x x xx
x x      x
x xxxxxx x
x    x    
xxxxxxxxxx


and my code is:

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
template<class ItemType>
bool Maze<ItemType>::load(const std::string fn)
{
	//ItemType line;
	std::ifstream inFile(fn);
	//while(inFile.peek() != EOF){
		n=inFile.get();
		inFile.ignore(1);
		m=inFile.get();
		//matrix = new ItemType[m][n]; // Adjust matrix dimensions
		startX=inFile.get();
		inFile.ignore(1);
		startY=inFile.get();
		endX=inFile.get();
		inFile.ignore(1);
		endY=inFile.get();
	for (int i=0; i < n; i++) {
		for (int j=0; j < m; j++) {
			inFile >> matrix[i][j];
		}
	}
		//getline(inFile, line);
		//add(line);
	//} //end while
	return (inFile.is_open());
	inFile.close();
}


I think I'm doing something wrong because when I try to display matrix[][], it shows something weird. Thanks in advance.
I guess the first line is for cols and rows.
What do the numbers on line 2 and 3 mean?
Hints:
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <fstream>
#include <iostream>
#include <sstream>
#include <limits>
#include <string>
#include <vector>

void waitForEnter();

int main()
{
    std::ifstream infile("maze.in");
    std::string line;
    std::getline(infile, line); // gets rid of '\n'
    std::stringstream iss(line);
    int n {}, m {};
    iss >> n >> m; // if you called it 'cols' and 'rows', you'd make less errors
    
    // Problem: declaring a 2-D C-style array
    // Option 1 (the best): use std::vector and get rid of every problems
    std::vector<std::vector<int>> matrix (m, std::vector<int>(n));
    // Option 2:
    int* matrix2 = new int[m*n] {}; // all memory in subsequent cells
    // Option 3:
    int** matrix3 = new int*[m] {};
    for(int i{}; i<m; ++i) {
        matrix3[i] = new int[n] {}; // every 'row' could lie in different memory areas
    }

    // Let's read the other two lines of numbers, whatever they mean
    std::getline(infile, line);
    iss.str(line);
    int startX {}, startY {};
    iss >> startX >> startY;
    std::getline(infile, line);
    iss.str(line);
    int endX {}, endY {};
    iss >> endX >> endY;

    for (int i=0; i < m; i++) {
        std::getline(infile, line);
        for (size_t j=0; j < line.length(); j++) {
            matrix.at(i).at(j) = line.at(j);
            matrix2[i*n+j] = line.at(j);
            matrix3[i][j] = line.at(j);
        }
    }

    infile.close();

    // let's check if we've read everything correctly:
    std::cout << "\nstd::vector:\n";
    for(const auto& v : matrix) {
        for(const auto i : v) { std::cout << char(i) << ' '; }
        std::cout << '\n';
    }

    std::cout << "\nsingle pointer:\n";
    for (int i=0; i < m; i++) {
        for (int j=0; j < n; j++) {
            std::cout << char(matrix2[i*n+j]) << ' ';
        }
        std::cout << '\n';
    }

    std::cout << "\npointer to pointer:\n";
    for (int i=0; i < m; i++) {
        for (int j=0; j < n; j++) {
            std::cout << char(matrix3[i][j]) << ' ';
        }
        std::cout << '\n';
    }

    // Let's get to the hard job: avoid memory leaks:
    // matrix: <-- will be correctly terminated by its destructor
    delete[] matrix2;
    for(int i{}; i<m; ++i) { delete[] matrix3[i]; }

    std::cout << '\n';
    waitForEnter();
    return 0;   
}

void waitForEnter()
{
    std::cout << "\nPress ENTER to continue...\n";
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}



std::vector:
x x x x x x x x   x
x       x   x     x
x   x   x   x   x x
x   x             x
x   x x x x x x   x
x         x
x x x x x x x x x x

single pointer:
x x x x x x x x   x
x       x   x     x
x   x   x   x   x x
x   x             x
x   x x x x x x   x
x         x
x x x x x x x x x x

pointer to pointer:
x x x x x x x x   x
x       x   x     x
x   x   x   x   x x
x   x             x
x   x x x x x x   x
x         x
x x x x x x x x x x


Press ENTER to continue...

Topic archived. No new replies allowed.