简体   繁体   中英

Reading text file into a 2d array c++

 char initialMaze[ SIZEY+1][ SIZEX+1]  //local array to store the maze structure
          = { {'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X'},
              {'X', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'},
              {'X', '#', '#', '#', '#', '#', ' ', ' ', ' ', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'},
              {'X', '#', '#', '#', '#', '#', ' ', ' ', ' ', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'},
              {'X', '#', '#', '#', '#', '#', ' ', ' ', ' ', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'},
              {'X', '#', '#', '#', ' ', ' ', ' ', ' ', ' ', ' ', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'},
              {'X', '#', '#', '#', ' ', '#', ' ', '#', '#', ' ', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'},
              {'X', '#', ' ', ' ', ' ', '#', ' ', '#', '#', ' ', '#', '#', '#', '#', '#', ' ', ' ', '+', '+', '#'},
              {'X', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '+', '+', '#'},
              {'X', '#', '#', '#', '#', '#', ' ', '#', '#', '#', ' ', '#', ' ', '#', '#', ' ', ' ', '+', '+', '#'},
              {'X', '#', '#', '#', '#', '#', ' ', ' ', ' ', ' ', ' ', '#', '#', '#', '#', '#', '#', '#', '#', '#'},
              {'X', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'},             };

Hi, I'm trying to create some code that will load this maze layout from a .txt file and put it into a 2d array. Currently it just puts the maze layout straight into the 2d array without having it stored in a .txt file. Does anyone know how I will do this?

Step 1: Create a text file which looks like:

XXXXXXXXXXXXXXXXXXXX
X###################
X#####   ###########
X#####   ###########
X#####   ###########
X###      ##########
X### # ## ##########
X#   # ## #####  ++#
X#               ++#
X##### ### # ##  ++#
X#####     #########
X###################

Step 2: write the code for reading the maze text file

char initialMaze[SIZEY+1][SIZEX+1];
int row = 0;
ifstream fstrm("filename.txt");
while(fstrm.getline(initialMaze[row], SIZEX+1)) {
    ++row;
}

If the text representing the maze is in a text file called maze.txt, Then the following code might be sufficient,

  char initialMaze[SIZEY+1][ SIZEX+1];
  string temp;
  int i=0;
  ifstream var("maze.txt");
  if (myfile.is_open())
  {
    while(getline(var,temp) )
    {
      strcpy(initialMaze[i],temp.c_str());
      i++;
    }
    myfile.close();
  }

maze.txt:

XXXXXXXXXXXXXXXXXXXX
X###################
X#####   ###########
X#####   ###########
X#####   ###########
X###      ##########
X### # ## ##########
X#   # ## #####  ++#
X#               ++#
X##### ### # ##  ++#
X#####     #########
X###################

maze.cpp:

#include <fstream>
#include <string>
#include <vector>
#include <cstdlib>

int main() {
  std::ifstream fin("maze.txt");
  if (!fin) return EXIT_FAILURE;

  std::vector<std::string> maze;
  std::string line;
  while (std::getline(fin, line)) {
    maze.push_back(line);
  }
}

This gets the data into maze , where you can access any cell using maze[row][column] . The maze can be practically any size, and rows don't even have to all be the same length. (Just be sure when you access an element that the row and column are inside the maze:

if (0 <= row && row < maze.size() && 0 <= column && column < maze[row].size()) {
    maze[row][column] ...
}

text.txt

1880    1   0   67.50   10.50   -1.00   -1.00
1880    1   4   66.50   11.50   -1.00   -1.00
1880    1   8   66.50   11.50   -1.00   -1.00
1880    1   12      65.50   11.50   -1.00   -1.00
1880    1   16      64.50   11.50   -1.00   -1.00
1880    1   20      63.50   12.50   -1.00   -1.00
1880    2   0   63.50   12.50   -1.00   -1.00
1880    2   4   62.50   12.50   -1.00   -1.00
1880    2   8   62.50   12.50   -1.00   -1.00

text.cpp

#include <vector>
#include <string>
#include <sstream>
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
    vector<vector<double> > data;
    ifstream file("D:\\test.txt");// file path
    string line;
    while (getline(file, line))
    {
        data.push_back(vector<double>());
        istringstream ss(line);
        double value;
        while (ss >> value)
        {
            data.back().push_back(value);
        }
    }
    for (int y = 0; y < data.size(); y++)
    {
        for (int x = 0; x < data[y].size(); x++)
        {
            cout<<data[y][x]<< " ";
        }
        cout << endl;
    }
    return 0;
}

run result:

1880 1 0 67.5 10.5 -1 -1
1880 1 4 66.5 11.5 -1 -1
1880 1 8 66.5 11.5 -1 -1
1880 1 12 65.5 11.5 -1 -1
1880 1 16 64.5 11.5 -1 -1
1880 1 20 63.5 12.5 -1 -1
1880 2 0 63.5 12.5 -1 -1
1880 2 4 62.5 12.5 -1 -1
1880 2 8 62.5 12.5 -1 -1
Press any key to continue

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM