I'm working on two programs. The first generates a maze and saves the maze in a .txt-file. The maze in the .txt-file looks like this:
11111111111111111
19999999199999991
19191119991919191
11111111111111111
The second program uses the a-star algorithm to solves the maze. Therefore the program opens the file and reads the maze into a one-dimensional array.
To read the maze into the array I use the following code.
First the file is checked to calculate the needed size of the array. Then the maze should be transfered to the array.
ifstream myfile("BLOCK_style_maze.txt");
string line;
int colCount=0;
int rowCount=0;
int temp=0;
if(myfile.is_open())
{
if(getline(myfile,line))
{
rowCount++;
int i=0;
for(i=0;i<line.length();i++)
{
if(line.at(i)=='1' || line.at(i)=='9') colCount++;
}
}
while(getline(myfile, line))
{
rowCount++;
}
cout << "R:"<< rowCount << endl << "C:" << colCount << endl;
myfile.close();
}
else
{
cout << "Unabale to open maze file";
}
MAP_WIDTH = colCount;
MAP_HEIGHT = rowCount;
map=newint [MAP_WIDTH*MAP_HEIGHT];
int k=MAP_WIDTH*MAP_HEIGHT;
int j=0;
if (myfile.is_open())
{
while(myfile >> temp)
{
map[j++] = temp;
}
}
for(int i=0; i<=k; i++ )
{
cout << map[i]<< endl;
}
To check if everything is working I tried to print the array on the console but I just go 0 as output, but I expected only 1's and 9's. Since I'm not really experienced with programming in C++ I can't find my mistake.
@ Aceix: Thanks for your tip. But how can I implement the seekg() function in my program, or better where is my mistake. I mean I still don't get what I'm doing wrong.
So I tried it with a small test example like
1919191919
9191919191
But the output of the array was:
9993200
0
9992096
0
778398825
...
So I think I just get, what is left in the RAM as output, and so my function for reading to the array must be wrong
My thought were: First count the columns and rows to know how big the int-array have to be, than create the array in the heap, and at last open the file again read the integers character by character from the file and store it in the array.
Do you have another tip for me