Get() taking in newline

Hello. I am creating a program to read in a maze and solve it but when I read in the maze file into a dynamically allocated array using get I notice it reads the newline which messes up where everything is placed in the array and I'm not sure how to avoid this issue. Any Ideas?


1
2
3
4
5
6
7
8
9
10
11
12
 char** mazechars = new char*[temprow+1];
  for(int i = 0; i < temprow; i++){
     mazechars[i] = new char[tempcol+1];
   }
  //then this reads in the symbols
   ifstream maze(filename);
   for(int i = 0; i < temprow; i++){
       for(int j = 0; j <= tempcol+1; j++){
          maze.get(mazechars[i][j]);
          cout << mazechars[i][j] << " " << i << " " << j << endl;
        }
    } 


I was trying to debug and I saw this is what it printed out which is wrong
1
2
3
4
5
6
7
8
9
10
11
12
5 0 0
  0 1
5 0 2
  0 3
  0 4

 0 5
. 0 6
S 1 0
. 1 1
# 1 2
. 1 3
Update:

I didn't figure out how to get rid of the newline but I adjusted the size of my dynamically allocated array that way its 5x5
std::istream::get() is an unformatted input function; it reads in all characters including white space characters. A formatted input function (operator>>) would skip leading white space characters.

May be something like this:

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
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <string>
#include <vector>
#include <fstream>

using maze_t = std::vector<std::string> ;

maze_t read_maze( std::istream& stm )
{
    std::size_t nrows = 0 ;
    std::size_t ncols = 0 ;
    stm >> nrows >> ncols >> std::ws ; // read in the number of rows and cols, then skip to the next line

    maze_t maze(nrows) ; // create a maze containing nrows rows
    for( std::string& row : maze ) // for each row in the maze
    {
        std::getline( stm, row ) ; // read in the row
        row.resize( ncols, ' ' ) ; // adjust the number of cols (if required)
    }

    return maze ;
}

std::ostream& print_maze( const maze_t& maze, std::ostream& stm = std::cout )
{
    for( const std::string& row : maze ) // for each row in the maze
        stm << row << '\n' ; // print the row, followed by a new line

    return stm ;
}

int main() // simple test driver
{
    const std::string test_file_name = "test_maze.txt" ;
    // create a test file
    {
        std::ofstream(test_file_name) << "5 15\n" // 5 rows, 15 cols
                                         "!abcdefghijklm+\n" // row 1
                                         "!0123456789012+\n" // row 2
                                         "!TTTTTTTTTTTTT+\n" // row 3
                                         "!FFFFFFFFFFFFF+\n" // row 4
                                         "!5555555555555+\n" ; // row 5
    }

    if( std::ifstream file{test_file_name} )
    {
        const auto maze = read_maze(file) ;
        print_maze(maze) ;
    }
}

http://coliru.stacked-crooked.com/a/36c4bf059935aa1b
You should use getline() instead of get().
Below a simple example showing how to manage strings in a file, but it means that your file must respect maze size ++

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

int main() {

    std::string buffer;
    std::vector<std::string> lines;

    std::ifstream myfile("file.txt");

    if (myfile.is_open())
    {
        while (getline(myfile, buffer))
            lines.push_back(buffer);

        myfile.close();
    }
    else
        std::cout << "Unable to open file";

    for (auto x : lines)
        std::cout << x << "\n";

    return 0;
}
Last edited on
Or even:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <iterator>

int main() {
	std::vector<std::string> lines;

	if (std::ifstream myfile { "file.txt" })
		for (std::string buffer; getline(myfile, buffer); lines.push_back(std::move(buffer)));
	else
		std::cout << "Unable to open file\n";

	for (const auto& x : lines)
		std::cout << x << '\n';
}

Topic archived. No new replies allowed.