File stream read in error

Hello!
Context: I am making a simple code to read in a maze from a file
the maze should look like this:
1
2
3
4
5
6
5 5
.S.#. 
##.#.
.....
.####
....F


but instead, it looks like this:
1
2
3
4
5
6
7
8
9
10
5 5
.S.#. 
##.#.
.....
.####
....F

##.#.
.....
.# 


which causes errors later on in my code is anyone able to recognize the error from this part of my code?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  //dynamically allocates array
  char** mazechars = new char*[*rows+1];
  for(int i = 0; i < *rows; i++){
     mazechars[i] = new char[*cols];
   }
  //then this reads in the symbols
   ifstream maze(filename);
   while(!maze.eof()){
     for(int i = 0; i < *rows; i++){
       for(int j = 0; j < *cols; j++){
        maze.get(mazechars[i][j]);
        cout << mazechars[i][j];
       }
     }
   }


thank you greatly
Last edited on
First, read this https://cplusplus.com/articles/jEywvCM9/
Learn how to use code tags when posting code.

> ifstream maze(filename);
You're supposed to have read rows and cols from the file before this, so why are you opening the file again?


> maze.get(mazechars[i][j]);
You're also reading all the newline characters at the end of each line.

> while(!maze.eof()){
The file is read once by the two loops, you don't need another loop as well.
Thank you and also I added the code tags ty for the article
Topic archived. No new replies allowed.