繁体   English   中英

将文本文件读入二维数组C ++

[英]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', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'},             };

嗨,我正在尝试创建一些代码,这些代码将从.txt文件加载此迷宫布局,并将其放入2d数组中。 目前,它只是将迷宫布局直接放入2d数组中,而没有将其存储在.txt文件中。 有人知道我会怎么做吗?

步骤1:创建一个文本文件,如下所示:

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

步骤2:编写用于读取迷宫文本文件的代码

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

如果表示迷宫的文本在名为maze.txt的文本文件中,则下面的代码可能就足够了,

  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);
  }
}

这会将数据放入maze ,您可以在其中使用maze[row][column]来访问任何单元。 迷宫几乎可以是任何大小,行甚至不必都具有相同的长度。 (仅当访问元素时,请确保行和列在迷宫中:

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;
}

运行结果:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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