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
#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.
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;
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++.
@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: