gpt4 book ai didi

c++ - 将 ascii 'maze' 读入二维数组

转载 作者:搜寻专家 更新时间:2023-10-31 02:00:48 24 4
gpt4 key购买 nike

我正在编写代码以读取代表“迷宫”的文件中的 7x15 文本 block 。

#include <iostream>
#include <fstream>
#include <string>
#include "board.h"

int main()
{
char charBoard[7][15]; //the array we will use to scan the maze and modify it
ifstream loadMaze("maze"); //the fstream we will use to take in a maze
char temp; //our temperary holder of each char we read in

for(int i = 0;i < 7; i++)
{

for(int j = 0; j < 15; j++)
{
temp= loadMaze.get();
charBoard[i][j] = temp;
cout << charBoard[i][j]; //testing
}
cout << endl;
}

return 0;
}

这是我的原始草稿,但由于它不断返回而没有用?对于它读取的每个字符。这是我正在测试的迷宫:

  #############              #############  #              # ######### #### # !       #   ############   

编辑:cout 正在打印:

  ##########################  #  # ######### #### # !       #   #########

我不是在转义\n 吗?

我已经编写了几个小时的代码,所以我认为这是一个我没有发现的简单错误,这让我现在很困惑。谢谢!

最佳答案

尝试像“c:\MyMazes\maze”这样的绝对路径。

输入系统(“cd”)以查看当前目录在哪里。如果您在查找当前目录时遇到问题,请查看此 SO discussion

这是完整的代码 - 这应该显示您的整个迷宫(如果可能)和当前目录。

 char charBoard[7][15];      //the array we will use to scan the maze and modify it
system("cd");
ifstream loadMaze("c:\\MyMazes\\maze"); //the fstream we will use to take in a maze

if(!loadMaze.fail())
{
for(int i = 0;i < 7; i++)
{
// Display a new line
cout<<endl;
for(int j = 0; j < 15; j++)
{
//Read the maze character
loadMaze.get(charBoard[i][j]);
cout << charBoard[i][j]; //testing
}
// Read the newline
loadMaze.get();
}
return 0;
}
return 1;

关于c++ - 将 ascii 'maze' 读入二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1541763/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com