Create two dimensional array from file

Given a file of n lines with n characters per line how do I create a two dimensional array from it?
Maybe something like:

std::vector<std::vector<int>> vint(n, std::vector<int>(n));
There may be some ideas in this thread:
http://www.cplusplus.com/forum/general/178865/
jlb that's what I'm using but I'm having trouble actually transferring the information to the array

this is my code so far
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
#include <iostream>
using namespace std;
#include<fstream>
#include<vector>

int main()
{
	ofstream infile;
	infile.open("maze.txt");
	vector<vector<int>> maze;
	string temp;

	if (infile.good)
	{
		char temp;
		for (int i = 0; i < getline; i++) //this is where im having trouble
		{
			for (int j = 0; j < maze.size(); j++)
			{
				infile << maze[i][j]; 
			}
			
		}
	
	}
}


Chervil how would I implement the code you provided in the link with my code?
Please post a sample of your input file.

And note that you can't use array notation if the vector is empty, you first need to insert some elements into the vector.
Chervil how would I implement the code you provided in the link with my code?

Well, it would replace most of your code. Of course you need the relevant #includes and point it to the correct input file.


By the in order to read from an input file, use ifstream. You have currently ofstream which is for writing to an output file.
Last edited on
This is the file

XXSXXX
X0000X
XXX00X
X0X0XX
X0000X
XXXXEX
Chervil, what are the advantages of using a vector of strings rather than a 2d vector?

There may be some disadvantages, in particular all rows may not be the same length.

But one advantage is that the whole thing can be printed out like this:
1
2
    for (string & s : array)
        cout << s << endl;


The only reason I suggested it is that a string is in effect an array of characters (with additional features which can be ignored). So that gives the x-dimension of the array. Each element of the vector would give the y-dimension.

You can still access individual elements by row and column, for example like this, print out the array one character at a time.
1
2
3
4
5
6
7
	
    for (int row=0; row <array.size(); row++)
    {
        for (int col=0; col<array[row].size(); col++)
            cout << array[row][col];
        cout << endl;
    }



I'm not sure I'd recommend it, it was just an idea. Another idea is in this post:
http://www.cplusplus.com/forum/beginner/180236/#msg885131

I tried it, it compiles but does not print.

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
#include <iostream>
using namespace std;
#include<fstream>
#include<vector>
#include<string>

int main()
{
	ifstream infile;
	infile.open("maze.txt");
	vector<string>maze1;
	string temp;

	while (getline(infile, temp))
		maze1.push_back(temp);

        //print
	for (string & s : maze1)
		cout << s << endl;

        //print
	for (int i = 0; i < maze1.size(); i++)
	{
		for (int j =0; j < maze1.size(); j++)
		{
			cout << maze1[2][3] << endl;
			cout << "hello";
		}
	}
}

Last edited on
The most likely reason nothing was printed was that nothing was extracted from the file... and you make no effort to ensure the file is opened.


1
2
3
4
5
6
7
8
9
10
11
12
13
    // ...
    ifstream infile;
    infile.open("maze.txt");

    if (!infile.is_open())
    {
        std::cerr << "Sorry!  Unable to open file!\n" ;
        return 0;
    }

    vector<string>maze1;
    string temp;
    // ... 

1
2
while (getline(infile, temp))
		maze1.push_back(temp);


I thought that took care of extracting from the file into the vector
I thought that took care of extracting from the file into the vector

If the file is never opened, I'm afraid getline will never succeed and nothing will ever be added to maze1.
my bad, the file was empty
Topic archived. No new replies allowed.