reading file into a 2-d array

i got a maze program to do, and we are supposed to read a maze from a .txt file and i have no clue on how to do that. I feel okay on the rest of the parts, but just reading it im lost. the file will have a the size of the array, then the following lines are the rows and columns. i prefer in C language but c++ is fine too

5 10
xxxxxxxxxx
xs xxx xsx
x x
x f x
xxxxxxxxxx

thats one maze that we have to try out.
s - start
f- finish
x - wall
whitespace is clear path

thanks in advance.
the last x's in the 3 middle rows is are supposed to be all the way to the right. and the f is supposed to be in the mid of the 4th row.
we are supposed to read a maze from a .txt file and i have no clue on how to do that


Check out C++ file IO: http://pages.cs.wisc.edu/~hasti/cs368/CppTutorial/NOTES/IO.html#files
I do not know if the same can be done with C, but this tutorial got me up and running with reading and writing to files in C++

Onions.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <fstream>
ifstream maze;
char mazeArray[24][80]; //since thats my max size maze
maze.open("maze.txt", ios::in);
if (!"maze.txt") {
     cerr << "unable to open file maze.txt for reading" << endl;
     exit(1);
}
else
{
while("maze.txt" >> mazeArray[][])
// not sure what to do here.
}
  


This is what i got so far, but its not correct, i think i understand how to open the file, but to make it into an array im lost.
First of all you probably want:
1
2
3
4
if(!maze.open("maze.txt",ios::in))
{
         //error handling code
}


In terms of reading the file itself into an array, look over the functions on this site:
http://www.cplusplus.com/reference/iostream/fstream/
You'll want what's under the heading "ifstream".
One place to start might be the getline function?

So this is what i got so far, i read the first two integers (rows, columns), but after that i dont know how to start from the next line and make the 2d array. and i looked into getline() and i feel that it would help me make a 1-d array, not sure how to go about the 2d;
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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

void copyMaze(string file, int& mazeRow, int& mazeColumn)
{
    ifstream in_file;
    in_file.open(file.c_str());
    if(in_file.fail())
        {
            cerr << "File did not open!" << endl;
            return;
                    }
    else
    {
        in_file >> mazeRow >> mazeColumn;
        char maze[mazeRow][mazeColumn];
    }

}


int main(int argc, char* argv[])
{
    int mazeRow = 24, mazeColumn = 80;
    char maze[mazeRow][mazeColumn];
    string file;
    cout << "Enter maze file name." << endl;
    cin >> file;

    copyMaze(file, mazeRow,mazeColumn);


    cout << "Rows: " << mazeRow << "\nColumns: "<< mazeColumn << endl;
    return 0;
}

How is the file formatted? Any examples of what will be in side the .txt files?
Your main problems are:
1. You need to set each element of "maze" instead of just declaring it.
2. "maze" cannot be easily declared with size [mazeRow][mazeColumn]. You need to set it to a large value at the start.
3. You need to pass the address of "maze" into the function or you are only making a temporary variable that will be destroyed when the function ends. I believe this is the same in C and C++.

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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

void copyMaze(string file, char *maze[][], int *mazeRow, int *mazeColumn)
{
    ifstream in_file;
    in_file.open(file.c_str());
    if(in_file.fail())
    {
        cerr << "File did not open!" << endl;
        return;
    }
    else
    {
        in_file >> *mazeRow >> *mazeColumn;
	for (int x = 0, x< *mazeRow, x++)
		for(int y = 0, y < *mazeColumn, y++)
			in_file>> *maze[x][y]; //I believe you need to set each element seperately.
    }
}

int main(int argc, char* argv[])
{
    char maze[100][100] = {0};
    int mazeRow, mazeColumn;
    string file;
    cout << "Enter maze file name." << endl;
    cin >> file;

    copyMaze(file, &maze, &mazeRow, &mazeColumn);

    cout << "Rows: " << mazeRow << "\nColumns: "<< mazeColumn << endl;
    return 0;
}
Last edited on
@Onions: He did give an example :P Right in the OP.

@ale: Since it's obvious you're not just trying to get us to do your homework I'll try
to help you a little more.
One way to make the char array maze be the exact size given is to make it an array
pointer and read the maze dimensions in before creating it:

1
2
3
4
5
6
7
8
char* maze;
ifstream file("Myfile");
if(file.fail())
  {
     //Error
  }
file>>mazeRow>>mazeColumn;
maze = new char[mazeRow*mazeColumn];


Then, using the getline function you can read in each line of the file separately
and assign it to the proper position in maze:
1
2
3
4
std::getline(file,mystring);
for(int i = 0;i<mystring.size();i++){
maze[i*currentRow] = mystring[i];
}
Topic archived. No new replies allowed.