C++: Read text file into 2d array












-4















I'm trying to read the following maze.txt file:



35
35
0
10
++++++++++S++++++++++++++++++++++++
++++++++++OOOOOOOOOOOOOOOOOOOOOOOOO
++++++++++O++++++++++++++++++O+++++
OOOOOOOOOOOOOOOOOOOOOOOOOO+++O++OOE
O+++++++++O++++++++++++++O+++O++O++
OOOOOO++++O++++++++++++++O+++O++O++
O++++O++++OOOOOOOOOOO++++O+OOO++O++
O++++O++++O+++++++++OOOO+O+++O++O++
OOO++OOOO+OOOOOO+++++++++++OOO++OOO
O+O+++++O++++++OOOOOOOOOO++O++++++O
O+OOOO++O++++++O++++++++O+++OOO+++O
O++++O++OOOOOOOO++++++++O+++O+O+++O
OOO++O++++++++++++++++++OOOOO+O+++O
++O++OOOOOOOOOOOOOOOO+++++++++OO++O
OOO+++++++++++++++++OOOOOO++++++++O
O++++++++++++++++++++++++O++OOOOOOO
+++++++++++++++++++++++++O++O++++++
OOOOOOOOOOOOOOOOOOOOOOOOOO++OOOOO++
O++++++++++++++++++++++++O++++++O++
OOOOOOO+++++++++++++++OOOOOOO+++O++
++++++++++++++++++++++O+++++OO++O++
OOOOOOOOOOOOOOOOOOOOOOO++++++O++O++
O++++++++++++++++++++++++++++O+OOOO
OOOO++++++++++++++++++++OOOOOO+O+++
+++OOOOOOOOO+++++++++++++++++++O+++
+++++O+++++OOOOOOOOOO++++++++OOO+++
+O+++OOOOO++++++O++++++++++++O+++++
+OOOOO+++O++++++OOOOOO+++++++O+++++
+++++++++++++++++++++OOOOOOOOO+++++
OOOOOOOOOOOOOOOOOOOOOO+++++++++++++
+++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++


The code works fine with the maze inside the code but I moved it out to a text file, which seems to be read but it is not working. It's giving me the error:



No matching function for call to 'mazeTravel'.


I'm not sure where to go from here. Any help would be appreciated!



#include <iostream>
#include <fstream>

using namespace std;

void printMaze(const char maze[12], int xCoordinate, int yCoordinate);
int mazeTravel(char maze[12], int xCoordinate, int yCoordinate, int direction);

int main()
{
char maze[35][35];
ifstream file;
file.open("maze.txt");
if (!file) {
cout << "Error reading filen";
return -1;
}
else {
for (int row = 0; row < 35; row++) {
for (int column = 0; column < 35; column++) {
file >> maze[row][column];

int success = 0;
success = mazeTravel(maze, 2, 0, 1);
if (success == 1)
cout << "The maze has been solved.n";
else
cout << "Sorry, the maze cannot be solvedn";
}
}
}
return 0;
}









share|improve this question




















  • 1





    You have to read the first some numbers at the beginning of the file. 35 35 0 10 . After that you can read the matrix from the file.

    – Peter
    Nov 27 '18 at 13:31













  • Please don't post pictures of text. Include the file in your question.

    – Swordfish
    Nov 27 '18 at 13:31













  • @Peter That may be my next problem, but I don't think that's why I'm getting that error message, any thoughts?

    – RochNoure
    Nov 27 '18 at 13:41











  • @Swordfish I edited my question

    – RochNoure
    Nov 27 '18 at 13:41






  • 1





    No matching function for call to 'mazeTravel' in main you have char maze[35][35]; not [something][12] that you have here int mazeTravel(char maze[12], int xCoordinate, int yCoordinate, int direction);

    – drescherjm
    Nov 27 '18 at 14:19


















-4















I'm trying to read the following maze.txt file:



35
35
0
10
++++++++++S++++++++++++++++++++++++
++++++++++OOOOOOOOOOOOOOOOOOOOOOOOO
++++++++++O++++++++++++++++++O+++++
OOOOOOOOOOOOOOOOOOOOOOOOOO+++O++OOE
O+++++++++O++++++++++++++O+++O++O++
OOOOOO++++O++++++++++++++O+++O++O++
O++++O++++OOOOOOOOOOO++++O+OOO++O++
O++++O++++O+++++++++OOOO+O+++O++O++
OOO++OOOO+OOOOOO+++++++++++OOO++OOO
O+O+++++O++++++OOOOOOOOOO++O++++++O
O+OOOO++O++++++O++++++++O+++OOO+++O
O++++O++OOOOOOOO++++++++O+++O+O+++O
OOO++O++++++++++++++++++OOOOO+O+++O
++O++OOOOOOOOOOOOOOOO+++++++++OO++O
OOO+++++++++++++++++OOOOOO++++++++O
O++++++++++++++++++++++++O++OOOOOOO
+++++++++++++++++++++++++O++O++++++
OOOOOOOOOOOOOOOOOOOOOOOOOO++OOOOO++
O++++++++++++++++++++++++O++++++O++
OOOOOOO+++++++++++++++OOOOOOO+++O++
++++++++++++++++++++++O+++++OO++O++
OOOOOOOOOOOOOOOOOOOOOOO++++++O++O++
O++++++++++++++++++++++++++++O+OOOO
OOOO++++++++++++++++++++OOOOOO+O+++
+++OOOOOOOOO+++++++++++++++++++O+++
+++++O+++++OOOOOOOOOO++++++++OOO+++
+O+++OOOOO++++++O++++++++++++O+++++
+OOOOO+++O++++++OOOOOO+++++++O+++++
+++++++++++++++++++++OOOOOOOOO+++++
OOOOOOOOOOOOOOOOOOOOOO+++++++++++++
+++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++


The code works fine with the maze inside the code but I moved it out to a text file, which seems to be read but it is not working. It's giving me the error:



No matching function for call to 'mazeTravel'.


I'm not sure where to go from here. Any help would be appreciated!



#include <iostream>
#include <fstream>

using namespace std;

void printMaze(const char maze[12], int xCoordinate, int yCoordinate);
int mazeTravel(char maze[12], int xCoordinate, int yCoordinate, int direction);

int main()
{
char maze[35][35];
ifstream file;
file.open("maze.txt");
if (!file) {
cout << "Error reading filen";
return -1;
}
else {
for (int row = 0; row < 35; row++) {
for (int column = 0; column < 35; column++) {
file >> maze[row][column];

int success = 0;
success = mazeTravel(maze, 2, 0, 1);
if (success == 1)
cout << "The maze has been solved.n";
else
cout << "Sorry, the maze cannot be solvedn";
}
}
}
return 0;
}









share|improve this question




















  • 1





    You have to read the first some numbers at the beginning of the file. 35 35 0 10 . After that you can read the matrix from the file.

    – Peter
    Nov 27 '18 at 13:31













  • Please don't post pictures of text. Include the file in your question.

    – Swordfish
    Nov 27 '18 at 13:31













  • @Peter That may be my next problem, but I don't think that's why I'm getting that error message, any thoughts?

    – RochNoure
    Nov 27 '18 at 13:41











  • @Swordfish I edited my question

    – RochNoure
    Nov 27 '18 at 13:41






  • 1





    No matching function for call to 'mazeTravel' in main you have char maze[35][35]; not [something][12] that you have here int mazeTravel(char maze[12], int xCoordinate, int yCoordinate, int direction);

    – drescherjm
    Nov 27 '18 at 14:19
















-4












-4








-4








I'm trying to read the following maze.txt file:



35
35
0
10
++++++++++S++++++++++++++++++++++++
++++++++++OOOOOOOOOOOOOOOOOOOOOOOOO
++++++++++O++++++++++++++++++O+++++
OOOOOOOOOOOOOOOOOOOOOOOOOO+++O++OOE
O+++++++++O++++++++++++++O+++O++O++
OOOOOO++++O++++++++++++++O+++O++O++
O++++O++++OOOOOOOOOOO++++O+OOO++O++
O++++O++++O+++++++++OOOO+O+++O++O++
OOO++OOOO+OOOOOO+++++++++++OOO++OOO
O+O+++++O++++++OOOOOOOOOO++O++++++O
O+OOOO++O++++++O++++++++O+++OOO+++O
O++++O++OOOOOOOO++++++++O+++O+O+++O
OOO++O++++++++++++++++++OOOOO+O+++O
++O++OOOOOOOOOOOOOOOO+++++++++OO++O
OOO+++++++++++++++++OOOOOO++++++++O
O++++++++++++++++++++++++O++OOOOOOO
+++++++++++++++++++++++++O++O++++++
OOOOOOOOOOOOOOOOOOOOOOOOOO++OOOOO++
O++++++++++++++++++++++++O++++++O++
OOOOOOO+++++++++++++++OOOOOOO+++O++
++++++++++++++++++++++O+++++OO++O++
OOOOOOOOOOOOOOOOOOOOOOO++++++O++O++
O++++++++++++++++++++++++++++O+OOOO
OOOO++++++++++++++++++++OOOOOO+O+++
+++OOOOOOOOO+++++++++++++++++++O+++
+++++O+++++OOOOOOOOOO++++++++OOO+++
+O+++OOOOO++++++O++++++++++++O+++++
+OOOOO+++O++++++OOOOOO+++++++O+++++
+++++++++++++++++++++OOOOOOOOO+++++
OOOOOOOOOOOOOOOOOOOOOO+++++++++++++
+++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++


The code works fine with the maze inside the code but I moved it out to a text file, which seems to be read but it is not working. It's giving me the error:



No matching function for call to 'mazeTravel'.


I'm not sure where to go from here. Any help would be appreciated!



#include <iostream>
#include <fstream>

using namespace std;

void printMaze(const char maze[12], int xCoordinate, int yCoordinate);
int mazeTravel(char maze[12], int xCoordinate, int yCoordinate, int direction);

int main()
{
char maze[35][35];
ifstream file;
file.open("maze.txt");
if (!file) {
cout << "Error reading filen";
return -1;
}
else {
for (int row = 0; row < 35; row++) {
for (int column = 0; column < 35; column++) {
file >> maze[row][column];

int success = 0;
success = mazeTravel(maze, 2, 0, 1);
if (success == 1)
cout << "The maze has been solved.n";
else
cout << "Sorry, the maze cannot be solvedn";
}
}
}
return 0;
}









share|improve this question
















I'm trying to read the following maze.txt file:



35
35
0
10
++++++++++S++++++++++++++++++++++++
++++++++++OOOOOOOOOOOOOOOOOOOOOOOOO
++++++++++O++++++++++++++++++O+++++
OOOOOOOOOOOOOOOOOOOOOOOOOO+++O++OOE
O+++++++++O++++++++++++++O+++O++O++
OOOOOO++++O++++++++++++++O+++O++O++
O++++O++++OOOOOOOOOOO++++O+OOO++O++
O++++O++++O+++++++++OOOO+O+++O++O++
OOO++OOOO+OOOOOO+++++++++++OOO++OOO
O+O+++++O++++++OOOOOOOOOO++O++++++O
O+OOOO++O++++++O++++++++O+++OOO+++O
O++++O++OOOOOOOO++++++++O+++O+O+++O
OOO++O++++++++++++++++++OOOOO+O+++O
++O++OOOOOOOOOOOOOOOO+++++++++OO++O
OOO+++++++++++++++++OOOOOO++++++++O
O++++++++++++++++++++++++O++OOOOOOO
+++++++++++++++++++++++++O++O++++++
OOOOOOOOOOOOOOOOOOOOOOOOOO++OOOOO++
O++++++++++++++++++++++++O++++++O++
OOOOOOO+++++++++++++++OOOOOOO+++O++
++++++++++++++++++++++O+++++OO++O++
OOOOOOOOOOOOOOOOOOOOOOO++++++O++O++
O++++++++++++++++++++++++++++O+OOOO
OOOO++++++++++++++++++++OOOOOO+O+++
+++OOOOOOOOO+++++++++++++++++++O+++
+++++O+++++OOOOOOOOOO++++++++OOO+++
+O+++OOOOO++++++O++++++++++++O+++++
+OOOOO+++O++++++OOOOOO+++++++O+++++
+++++++++++++++++++++OOOOOOOOO+++++
OOOOOOOOOOOOOOOOOOOOOO+++++++++++++
+++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++


The code works fine with the maze inside the code but I moved it out to a text file, which seems to be read but it is not working. It's giving me the error:



No matching function for call to 'mazeTravel'.


I'm not sure where to go from here. Any help would be appreciated!



#include <iostream>
#include <fstream>

using namespace std;

void printMaze(const char maze[12], int xCoordinate, int yCoordinate);
int mazeTravel(char maze[12], int xCoordinate, int yCoordinate, int direction);

int main()
{
char maze[35][35];
ifstream file;
file.open("maze.txt");
if (!file) {
cout << "Error reading filen";
return -1;
}
else {
for (int row = 0; row < 35; row++) {
for (int column = 0; column < 35; column++) {
file >> maze[row][column];

int success = 0;
success = mazeTravel(maze, 2, 0, 1);
if (success == 1)
cout << "The maze has been solved.n";
else
cout << "Sorry, the maze cannot be solvedn";
}
}
}
return 0;
}






c++ maze






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 27 '18 at 14:27









Swordfish

10.2k11436




10.2k11436










asked Nov 27 '18 at 13:25









RochNoureRochNoure

75




75








  • 1





    You have to read the first some numbers at the beginning of the file. 35 35 0 10 . After that you can read the matrix from the file.

    – Peter
    Nov 27 '18 at 13:31













  • Please don't post pictures of text. Include the file in your question.

    – Swordfish
    Nov 27 '18 at 13:31













  • @Peter That may be my next problem, but I don't think that's why I'm getting that error message, any thoughts?

    – RochNoure
    Nov 27 '18 at 13:41











  • @Swordfish I edited my question

    – RochNoure
    Nov 27 '18 at 13:41






  • 1





    No matching function for call to 'mazeTravel' in main you have char maze[35][35]; not [something][12] that you have here int mazeTravel(char maze[12], int xCoordinate, int yCoordinate, int direction);

    – drescherjm
    Nov 27 '18 at 14:19
















  • 1





    You have to read the first some numbers at the beginning of the file. 35 35 0 10 . After that you can read the matrix from the file.

    – Peter
    Nov 27 '18 at 13:31













  • Please don't post pictures of text. Include the file in your question.

    – Swordfish
    Nov 27 '18 at 13:31













  • @Peter That may be my next problem, but I don't think that's why I'm getting that error message, any thoughts?

    – RochNoure
    Nov 27 '18 at 13:41











  • @Swordfish I edited my question

    – RochNoure
    Nov 27 '18 at 13:41






  • 1





    No matching function for call to 'mazeTravel' in main you have char maze[35][35]; not [something][12] that you have here int mazeTravel(char maze[12], int xCoordinate, int yCoordinate, int direction);

    – drescherjm
    Nov 27 '18 at 14:19










1




1





You have to read the first some numbers at the beginning of the file. 35 35 0 10 . After that you can read the matrix from the file.

– Peter
Nov 27 '18 at 13:31







You have to read the first some numbers at the beginning of the file. 35 35 0 10 . After that you can read the matrix from the file.

– Peter
Nov 27 '18 at 13:31















Please don't post pictures of text. Include the file in your question.

– Swordfish
Nov 27 '18 at 13:31







Please don't post pictures of text. Include the file in your question.

– Swordfish
Nov 27 '18 at 13:31















@Peter That may be my next problem, but I don't think that's why I'm getting that error message, any thoughts?

– RochNoure
Nov 27 '18 at 13:41





@Peter That may be my next problem, but I don't think that's why I'm getting that error message, any thoughts?

– RochNoure
Nov 27 '18 at 13:41













@Swordfish I edited my question

– RochNoure
Nov 27 '18 at 13:41





@Swordfish I edited my question

– RochNoure
Nov 27 '18 at 13:41




1




1





No matching function for call to 'mazeTravel' in main you have char maze[35][35]; not [something][12] that you have here int mazeTravel(char maze[12], int xCoordinate, int yCoordinate, int direction);

– drescherjm
Nov 27 '18 at 14:19







No matching function for call to 'mazeTravel' in main you have char maze[35][35]; not [something][12] that you have here int mazeTravel(char maze[12], int xCoordinate, int yCoordinate, int direction);

– drescherjm
Nov 27 '18 at 14:19














2 Answers
2






active

oldest

votes


















0














You could use a std::vector of std::strings to represent your maze:



std::vector<std::string> maze;


To access its cells use



maze[row][column];  // with row being y and column x


To get the number of rows use



maze.size()


and



maze[0].size()


for the number of columns.



You could read such a maze like that (without error checking to not clutter the code):



std::vector<std::string> readMaze(std::istream &is)
{
std::size_t columns;
std::size_t rows;
is >> columns >> rows;

int foo; // sorry, don't know what the 3rd and 4th
is >> foo >> foo; // number is. a starting position, perhaps?

// ignore the rest of the current line:
is.ignore(std::numeric_limits<std::streamsize>::max(), 'n');

std::string line;
std::vector<std::string> maze;

while (std::getline(is, line))
maze.push_back(line);

return maze;
}


An implementation (with error checking) could look like that:



#include <cstdlib>  // EXIT_FAILURE
#include <limits> // std::numeric_limits<>
#include <vector>
#include <string>
#include <fstream>
#include <iostream>

// to not have to type std::vector<std::string> all over the place
using maze_type = std::vector<std::string>;

void printMazeCell(maze_type const &maze, std::size_t x, std::size_t y)
{
std::cout.put(maze[y][x]);
}

void printMaze(maze_type const &maze)
{
for (auto const &row : maze)
std::cout << row << 'n';
}

int mazeTravel(maze_type const &maze, std::size_t x, std::size_t y, int dir)
{
// access cells of the maze with maze[y][x]
// maze.size() for the number of columns and
// maze[0].size() for the number of rows
return 42;
}

maze_type readMaze(std::istream &is)
{
std::size_t columns;
if (!(is >> columns)) {
std::cerr << "Couldn't read the number of columns :(nn";
return maze_type{}; // return an empty maze on error
}

std::size_t rows;
if (!(is >> rows)) {
std::cerr << "Couldn't read the number of rows :(nn";
return maze_type{};
}

int foo;
is >> foo >> foo; // sorry, don't know what the 3rd and 4th number is

// ignore the rest of the current line:
is.ignore(std::numeric_limits<std::streamsize>::max(), 'n');

std::cout << "Trying to read a maze with " << columns << " columns ...n";

std::string line;
maze_type maze;
while (std::getline(is, line)) {
if (line.length() != columns) {
std::cerr << "Found a row that contains only "
<< line.length() << " columns :(nn";
return maze_type{};
}
maze.push_back(line);
}

if (maze.size() != rows) {
std::cerr << "The maze only consists of "
<< maze.size() << " rows :(nn";
return maze_type{};
}

return maze;
}

int main()
{
char const *filename = "maze.txt";
std::ifstream is{ filename };
if (!is.is_open()) {
std::cerr << "Couldn't open "" << filename << "" for reading :(nn";
return EXIT_FAILURE;
}

maze_type maze = readMaze(is);

if (!maze.size()) { // readMaze returned an empty maze :(
std::cerr << "Bye.n";
return EXIT_FAILURE;
}

printMaze(maze);
}





share|improve this answer

































    0














    The problem that you don't have the implementation of



    int mazeTravel(char maze[12], int xCoordinate, int yCoordinate, int direction);


    You should create the implementation like this:



    int mazeTravel(char maze[12], int xCoordinate, int yCoordinate, int direction)
    {
    // The implementation
    return 0;
    }


    Another thing: You have to read the first some numbers at the beginning of the file.



    35
    35
    0
    10


    After that you can read the matrix from the file






    share|improve this answer
























    • I have the implementation as you pointed out, sorry for not including it as I was more focused on the file reading part.

      – RochNoure
      Nov 27 '18 at 14:04











    • Thank you for the help

      – RochNoure
      Nov 27 '18 at 22:48











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53500777%2fc-read-text-file-into-2d-array%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    You could use a std::vector of std::strings to represent your maze:



    std::vector<std::string> maze;


    To access its cells use



    maze[row][column];  // with row being y and column x


    To get the number of rows use



    maze.size()


    and



    maze[0].size()


    for the number of columns.



    You could read such a maze like that (without error checking to not clutter the code):



    std::vector<std::string> readMaze(std::istream &is)
    {
    std::size_t columns;
    std::size_t rows;
    is >> columns >> rows;

    int foo; // sorry, don't know what the 3rd and 4th
    is >> foo >> foo; // number is. a starting position, perhaps?

    // ignore the rest of the current line:
    is.ignore(std::numeric_limits<std::streamsize>::max(), 'n');

    std::string line;
    std::vector<std::string> maze;

    while (std::getline(is, line))
    maze.push_back(line);

    return maze;
    }


    An implementation (with error checking) could look like that:



    #include <cstdlib>  // EXIT_FAILURE
    #include <limits> // std::numeric_limits<>
    #include <vector>
    #include <string>
    #include <fstream>
    #include <iostream>

    // to not have to type std::vector<std::string> all over the place
    using maze_type = std::vector<std::string>;

    void printMazeCell(maze_type const &maze, std::size_t x, std::size_t y)
    {
    std::cout.put(maze[y][x]);
    }

    void printMaze(maze_type const &maze)
    {
    for (auto const &row : maze)
    std::cout << row << 'n';
    }

    int mazeTravel(maze_type const &maze, std::size_t x, std::size_t y, int dir)
    {
    // access cells of the maze with maze[y][x]
    // maze.size() for the number of columns and
    // maze[0].size() for the number of rows
    return 42;
    }

    maze_type readMaze(std::istream &is)
    {
    std::size_t columns;
    if (!(is >> columns)) {
    std::cerr << "Couldn't read the number of columns :(nn";
    return maze_type{}; // return an empty maze on error
    }

    std::size_t rows;
    if (!(is >> rows)) {
    std::cerr << "Couldn't read the number of rows :(nn";
    return maze_type{};
    }

    int foo;
    is >> foo >> foo; // sorry, don't know what the 3rd and 4th number is

    // ignore the rest of the current line:
    is.ignore(std::numeric_limits<std::streamsize>::max(), 'n');

    std::cout << "Trying to read a maze with " << columns << " columns ...n";

    std::string line;
    maze_type maze;
    while (std::getline(is, line)) {
    if (line.length() != columns) {
    std::cerr << "Found a row that contains only "
    << line.length() << " columns :(nn";
    return maze_type{};
    }
    maze.push_back(line);
    }

    if (maze.size() != rows) {
    std::cerr << "The maze only consists of "
    << maze.size() << " rows :(nn";
    return maze_type{};
    }

    return maze;
    }

    int main()
    {
    char const *filename = "maze.txt";
    std::ifstream is{ filename };
    if (!is.is_open()) {
    std::cerr << "Couldn't open "" << filename << "" for reading :(nn";
    return EXIT_FAILURE;
    }

    maze_type maze = readMaze(is);

    if (!maze.size()) { // readMaze returned an empty maze :(
    std::cerr << "Bye.n";
    return EXIT_FAILURE;
    }

    printMaze(maze);
    }





    share|improve this answer






























      0














      You could use a std::vector of std::strings to represent your maze:



      std::vector<std::string> maze;


      To access its cells use



      maze[row][column];  // with row being y and column x


      To get the number of rows use



      maze.size()


      and



      maze[0].size()


      for the number of columns.



      You could read such a maze like that (without error checking to not clutter the code):



      std::vector<std::string> readMaze(std::istream &is)
      {
      std::size_t columns;
      std::size_t rows;
      is >> columns >> rows;

      int foo; // sorry, don't know what the 3rd and 4th
      is >> foo >> foo; // number is. a starting position, perhaps?

      // ignore the rest of the current line:
      is.ignore(std::numeric_limits<std::streamsize>::max(), 'n');

      std::string line;
      std::vector<std::string> maze;

      while (std::getline(is, line))
      maze.push_back(line);

      return maze;
      }


      An implementation (with error checking) could look like that:



      #include <cstdlib>  // EXIT_FAILURE
      #include <limits> // std::numeric_limits<>
      #include <vector>
      #include <string>
      #include <fstream>
      #include <iostream>

      // to not have to type std::vector<std::string> all over the place
      using maze_type = std::vector<std::string>;

      void printMazeCell(maze_type const &maze, std::size_t x, std::size_t y)
      {
      std::cout.put(maze[y][x]);
      }

      void printMaze(maze_type const &maze)
      {
      for (auto const &row : maze)
      std::cout << row << 'n';
      }

      int mazeTravel(maze_type const &maze, std::size_t x, std::size_t y, int dir)
      {
      // access cells of the maze with maze[y][x]
      // maze.size() for the number of columns and
      // maze[0].size() for the number of rows
      return 42;
      }

      maze_type readMaze(std::istream &is)
      {
      std::size_t columns;
      if (!(is >> columns)) {
      std::cerr << "Couldn't read the number of columns :(nn";
      return maze_type{}; // return an empty maze on error
      }

      std::size_t rows;
      if (!(is >> rows)) {
      std::cerr << "Couldn't read the number of rows :(nn";
      return maze_type{};
      }

      int foo;
      is >> foo >> foo; // sorry, don't know what the 3rd and 4th number is

      // ignore the rest of the current line:
      is.ignore(std::numeric_limits<std::streamsize>::max(), 'n');

      std::cout << "Trying to read a maze with " << columns << " columns ...n";

      std::string line;
      maze_type maze;
      while (std::getline(is, line)) {
      if (line.length() != columns) {
      std::cerr << "Found a row that contains only "
      << line.length() << " columns :(nn";
      return maze_type{};
      }
      maze.push_back(line);
      }

      if (maze.size() != rows) {
      std::cerr << "The maze only consists of "
      << maze.size() << " rows :(nn";
      return maze_type{};
      }

      return maze;
      }

      int main()
      {
      char const *filename = "maze.txt";
      std::ifstream is{ filename };
      if (!is.is_open()) {
      std::cerr << "Couldn't open "" << filename << "" for reading :(nn";
      return EXIT_FAILURE;
      }

      maze_type maze = readMaze(is);

      if (!maze.size()) { // readMaze returned an empty maze :(
      std::cerr << "Bye.n";
      return EXIT_FAILURE;
      }

      printMaze(maze);
      }





      share|improve this answer




























        0












        0








        0







        You could use a std::vector of std::strings to represent your maze:



        std::vector<std::string> maze;


        To access its cells use



        maze[row][column];  // with row being y and column x


        To get the number of rows use



        maze.size()


        and



        maze[0].size()


        for the number of columns.



        You could read such a maze like that (without error checking to not clutter the code):



        std::vector<std::string> readMaze(std::istream &is)
        {
        std::size_t columns;
        std::size_t rows;
        is >> columns >> rows;

        int foo; // sorry, don't know what the 3rd and 4th
        is >> foo >> foo; // number is. a starting position, perhaps?

        // ignore the rest of the current line:
        is.ignore(std::numeric_limits<std::streamsize>::max(), 'n');

        std::string line;
        std::vector<std::string> maze;

        while (std::getline(is, line))
        maze.push_back(line);

        return maze;
        }


        An implementation (with error checking) could look like that:



        #include <cstdlib>  // EXIT_FAILURE
        #include <limits> // std::numeric_limits<>
        #include <vector>
        #include <string>
        #include <fstream>
        #include <iostream>

        // to not have to type std::vector<std::string> all over the place
        using maze_type = std::vector<std::string>;

        void printMazeCell(maze_type const &maze, std::size_t x, std::size_t y)
        {
        std::cout.put(maze[y][x]);
        }

        void printMaze(maze_type const &maze)
        {
        for (auto const &row : maze)
        std::cout << row << 'n';
        }

        int mazeTravel(maze_type const &maze, std::size_t x, std::size_t y, int dir)
        {
        // access cells of the maze with maze[y][x]
        // maze.size() for the number of columns and
        // maze[0].size() for the number of rows
        return 42;
        }

        maze_type readMaze(std::istream &is)
        {
        std::size_t columns;
        if (!(is >> columns)) {
        std::cerr << "Couldn't read the number of columns :(nn";
        return maze_type{}; // return an empty maze on error
        }

        std::size_t rows;
        if (!(is >> rows)) {
        std::cerr << "Couldn't read the number of rows :(nn";
        return maze_type{};
        }

        int foo;
        is >> foo >> foo; // sorry, don't know what the 3rd and 4th number is

        // ignore the rest of the current line:
        is.ignore(std::numeric_limits<std::streamsize>::max(), 'n');

        std::cout << "Trying to read a maze with " << columns << " columns ...n";

        std::string line;
        maze_type maze;
        while (std::getline(is, line)) {
        if (line.length() != columns) {
        std::cerr << "Found a row that contains only "
        << line.length() << " columns :(nn";
        return maze_type{};
        }
        maze.push_back(line);
        }

        if (maze.size() != rows) {
        std::cerr << "The maze only consists of "
        << maze.size() << " rows :(nn";
        return maze_type{};
        }

        return maze;
        }

        int main()
        {
        char const *filename = "maze.txt";
        std::ifstream is{ filename };
        if (!is.is_open()) {
        std::cerr << "Couldn't open "" << filename << "" for reading :(nn";
        return EXIT_FAILURE;
        }

        maze_type maze = readMaze(is);

        if (!maze.size()) { // readMaze returned an empty maze :(
        std::cerr << "Bye.n";
        return EXIT_FAILURE;
        }

        printMaze(maze);
        }





        share|improve this answer















        You could use a std::vector of std::strings to represent your maze:



        std::vector<std::string> maze;


        To access its cells use



        maze[row][column];  // with row being y and column x


        To get the number of rows use



        maze.size()


        and



        maze[0].size()


        for the number of columns.



        You could read such a maze like that (without error checking to not clutter the code):



        std::vector<std::string> readMaze(std::istream &is)
        {
        std::size_t columns;
        std::size_t rows;
        is >> columns >> rows;

        int foo; // sorry, don't know what the 3rd and 4th
        is >> foo >> foo; // number is. a starting position, perhaps?

        // ignore the rest of the current line:
        is.ignore(std::numeric_limits<std::streamsize>::max(), 'n');

        std::string line;
        std::vector<std::string> maze;

        while (std::getline(is, line))
        maze.push_back(line);

        return maze;
        }


        An implementation (with error checking) could look like that:



        #include <cstdlib>  // EXIT_FAILURE
        #include <limits> // std::numeric_limits<>
        #include <vector>
        #include <string>
        #include <fstream>
        #include <iostream>

        // to not have to type std::vector<std::string> all over the place
        using maze_type = std::vector<std::string>;

        void printMazeCell(maze_type const &maze, std::size_t x, std::size_t y)
        {
        std::cout.put(maze[y][x]);
        }

        void printMaze(maze_type const &maze)
        {
        for (auto const &row : maze)
        std::cout << row << 'n';
        }

        int mazeTravel(maze_type const &maze, std::size_t x, std::size_t y, int dir)
        {
        // access cells of the maze with maze[y][x]
        // maze.size() for the number of columns and
        // maze[0].size() for the number of rows
        return 42;
        }

        maze_type readMaze(std::istream &is)
        {
        std::size_t columns;
        if (!(is >> columns)) {
        std::cerr << "Couldn't read the number of columns :(nn";
        return maze_type{}; // return an empty maze on error
        }

        std::size_t rows;
        if (!(is >> rows)) {
        std::cerr << "Couldn't read the number of rows :(nn";
        return maze_type{};
        }

        int foo;
        is >> foo >> foo; // sorry, don't know what the 3rd and 4th number is

        // ignore the rest of the current line:
        is.ignore(std::numeric_limits<std::streamsize>::max(), 'n');

        std::cout << "Trying to read a maze with " << columns << " columns ...n";

        std::string line;
        maze_type maze;
        while (std::getline(is, line)) {
        if (line.length() != columns) {
        std::cerr << "Found a row that contains only "
        << line.length() << " columns :(nn";
        return maze_type{};
        }
        maze.push_back(line);
        }

        if (maze.size() != rows) {
        std::cerr << "The maze only consists of "
        << maze.size() << " rows :(nn";
        return maze_type{};
        }

        return maze;
        }

        int main()
        {
        char const *filename = "maze.txt";
        std::ifstream is{ filename };
        if (!is.is_open()) {
        std::cerr << "Couldn't open "" << filename << "" for reading :(nn";
        return EXIT_FAILURE;
        }

        maze_type maze = readMaze(is);

        if (!maze.size()) { // readMaze returned an empty maze :(
        std::cerr << "Bye.n";
        return EXIT_FAILURE;
        }

        printMaze(maze);
        }






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 27 '18 at 14:22

























        answered Nov 27 '18 at 14:15









        SwordfishSwordfish

        10.2k11436




        10.2k11436

























            0














            The problem that you don't have the implementation of



            int mazeTravel(char maze[12], int xCoordinate, int yCoordinate, int direction);


            You should create the implementation like this:



            int mazeTravel(char maze[12], int xCoordinate, int yCoordinate, int direction)
            {
            // The implementation
            return 0;
            }


            Another thing: You have to read the first some numbers at the beginning of the file.



            35
            35
            0
            10


            After that you can read the matrix from the file






            share|improve this answer
























            • I have the implementation as you pointed out, sorry for not including it as I was more focused on the file reading part.

              – RochNoure
              Nov 27 '18 at 14:04











            • Thank you for the help

              – RochNoure
              Nov 27 '18 at 22:48
















            0














            The problem that you don't have the implementation of



            int mazeTravel(char maze[12], int xCoordinate, int yCoordinate, int direction);


            You should create the implementation like this:



            int mazeTravel(char maze[12], int xCoordinate, int yCoordinate, int direction)
            {
            // The implementation
            return 0;
            }


            Another thing: You have to read the first some numbers at the beginning of the file.



            35
            35
            0
            10


            After that you can read the matrix from the file






            share|improve this answer
























            • I have the implementation as you pointed out, sorry for not including it as I was more focused on the file reading part.

              – RochNoure
              Nov 27 '18 at 14:04











            • Thank you for the help

              – RochNoure
              Nov 27 '18 at 22:48














            0












            0








            0







            The problem that you don't have the implementation of



            int mazeTravel(char maze[12], int xCoordinate, int yCoordinate, int direction);


            You should create the implementation like this:



            int mazeTravel(char maze[12], int xCoordinate, int yCoordinate, int direction)
            {
            // The implementation
            return 0;
            }


            Another thing: You have to read the first some numbers at the beginning of the file.



            35
            35
            0
            10


            After that you can read the matrix from the file






            share|improve this answer













            The problem that you don't have the implementation of



            int mazeTravel(char maze[12], int xCoordinate, int yCoordinate, int direction);


            You should create the implementation like this:



            int mazeTravel(char maze[12], int xCoordinate, int yCoordinate, int direction)
            {
            // The implementation
            return 0;
            }


            Another thing: You have to read the first some numbers at the beginning of the file.



            35
            35
            0
            10


            After that you can read the matrix from the file







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 27 '18 at 13:46









            PeterPeter

            62618




            62618













            • I have the implementation as you pointed out, sorry for not including it as I was more focused on the file reading part.

              – RochNoure
              Nov 27 '18 at 14:04











            • Thank you for the help

              – RochNoure
              Nov 27 '18 at 22:48



















            • I have the implementation as you pointed out, sorry for not including it as I was more focused on the file reading part.

              – RochNoure
              Nov 27 '18 at 14:04











            • Thank you for the help

              – RochNoure
              Nov 27 '18 at 22:48

















            I have the implementation as you pointed out, sorry for not including it as I was more focused on the file reading part.

            – RochNoure
            Nov 27 '18 at 14:04





            I have the implementation as you pointed out, sorry for not including it as I was more focused on the file reading part.

            – RochNoure
            Nov 27 '18 at 14:04













            Thank you for the help

            – RochNoure
            Nov 27 '18 at 22:48





            Thank you for the help

            – RochNoure
            Nov 27 '18 at 22:48


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53500777%2fc-read-text-file-into-2d-array%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

            Calculate evaluation metrics using cross_val_predict sklearn

            Insert data from modal to MySQL (multiple modal on website)