How do I read from a file and display it to the user?

I have a file (input2.txt) that looks like this: (https://imgur.com/a/Ey4qBpw )

And I want to open that file and store it in a 2d dynamic array. This is in the function main creating the dynamic array (dont ask why its dynamic even though its defined, idk why):(https://imgur.com/a/k0W2189 )

Here is opening the file, processing it, and (trying) to put the content into the array:(https://imgur.com/a/orA2Atq )

And the second part of my question is how would I display that array to the user? This is what I have for that function:(https://imgur.com/a/11cHp2K )

These are the library's that I have:(https://imgur.com/a/EgQPxM2 )

And finally, this is the output I currently get:(https://imgur.com/a/IBLEX24 )

Please ask me if you need any clarification about anything. Thanks for the help!
Last edited on
So, first you create a dynamically allocated 2d array, then open the file you want, then you read the information from the file and put it into you array. I assumed the your file has 8 lines and 8 columns as the image shows. If is not like that, you can change it fairly easy. Finally, you close the archive and free up the memory you allocated for your array. Thats it I guess, any doubt?

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

#include<iostream>
#include<fstream>

using namespace std;

int main() {
    
    int** matrix = new int*[8];
    for(int i = 0; i < 8; ++i){
	matrix[i] = new int[8];
    }    
    
    
    
    string s;
    cout << "type the name of the file: ";
    cin >> s;
    
    ifstream archive(s.c_str());
    

	int c;
	for (int i = 0; i < 8; i++) {
	    for (int j = 0; j < 8; j++) {
		archive >> c;
		matrix[i][j] = c;
	    }
	    
	}
    
    
    for (int i = 0; i < 8; i++) {
	for (int j = 0; j < 9; j++) {
	    cout << matrix[i][j] << " ";
	}
	cout << endl;
    }
    
    
    archive.close();
    
    for(int i = 0; i < 8; ++i) {
	delete [] matrix[i];
    }
    delete [] matrix;
    
    return 0;
}


If you don't understand anything in the code, just ask here. And also, if it solved the problem, remember to mark your question as solved please.
Yes! Thank you, that helped a lot!
Topic archived. No new replies allowed.