C++ 读取文本文件以填充 2D 数组
C++ Read a text file to populate a 2D array
所以我正在尝试在C++创建一个贪吃蛇游戏。玩家在开始不同难度的游戏时可以选择关卡。每个级别都存储在一个.txt文件中,我在从文件中填充数组时遇到问题。这是我到目前为止关于从文件中获取数组的代码。
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream fin("LevelEasy.txt");
fin >> noskipws;
char initialLevel[10][12];
for (int row = 0; row < 10; row++)
{
for (int col = 0; col < 12; col++)
{
fin >> initialLevel[row][col];
cout << initialLevel[row][col];
}
cout << "n";
}
system("pause");
return 0;
}
它填充第一行并完美打印。当它到达行尾时会出现问题,随后会导致之后每行出现问题。我希望它像这样打印;
############
# #
# #
# #
# #
# #
# #
# #
# #
############
但它最终只是打印了这样的东西;
############
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
###
我只是想知道在到达行尾后,我如何停止添加到数组的行并移动到下一个?任何帮助将不胜感激。
以下是我用来执行此操作的方法:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
int main() {
std::ifstream fin("LevelEasy.txt");
std::vector <std::string> initialLevel;
std::string line;
while(std::getline(fin,line)) {
initialLevel.push_back(line);
std::cout << line << 'n';
}
}
相关文章:
- 如何使用用户输入在C++中正确填充2D数组
- 创建整数的 2D 数组,该数组将使用两个函数用随机数填充矩阵.我做错了什么?
- 用空格填充 2D 数组(从文件输入)
- 填充包含碎屑的 2d 矢量
- C++初始化并用变量填充 2D 数组
- 填充另一个 2D 数组后,堆已损坏
- parallel_for lambda 函数填充 2D 向量后出错
- 填充 2D 阵列
- 如何有效地用枚举填充 2D std::数组
- C++ 读取文本文件以填充 2D 数组
- 从文件填充 2D 数组
- 在C 中填充2D矢量
- 用非重复的随机数填充 2D 数组
- 如何用每种可能的组合填充 2D 数组?C++逻辑
- 填充 2D 动态数组时"无法读取内存"
- 无法用文件中的sscanf_s填充 2D 数组
- C++ 用结构填充 2D 矢量
- c++11 使用 std::fill 来填充 2D 动态分配的数组
- C++填充2D阵列
- 如何用0和1填充2D数组,其中第N行的每个第N个元素都是1,其余元素是0