Create two dimensional array from file

Dec 6, 2015 at 5:38pm
Given a file of n lines with n characters per line how do I create a two dimensional array from it?
Dec 6, 2015 at 5:52pm
Maybe something like:

std::vector<std::vector<int>> vint(n, std::vector<int>(n));
Dec 6, 2015 at 6:00pm
There may be some ideas in this thread:
http://www.cplusplus.com/forum/general/178865/
Dec 6, 2015 at 10:11pm
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?
Dec 6, 2015 at 10:39pm
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.
Dec 6, 2015 at 10:56pm
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 Dec 6, 2015 at 10:58pm
Dec 6, 2015 at 10:59pm
This is the file

XXSXXX
X0000X
XXX00X
X0X0XX
X0000X
XXXXEX
Dec 6, 2015 at 11:13pm
Chervil, what are the advantages of using a vector of strings rather than a 2d vector?

Dec 6, 2015 at 11:31pm
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

Dec 6, 2015 at 11:52pm
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 Dec 6, 2015 at 11:53pm
Dec 6, 2015 at 11:58pm
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;
    // ... 

Dec 7, 2015 at 12:11am
1
2
while (getline(infile, temp))
		maze1.push_back(temp);


I thought that took care of extracting from the file into the vector
Dec 7, 2015 at 12:16am
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.
Dec 7, 2015 at 12:36am
my bad, the file was empty
Topic archived. No new replies allowed.