在 ascii “迷宫”中阅读进入二维数组

发布于 2024-08-07 00:10:26 字数 1012 浏览 5 评论 0原文

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

#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 吗?

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

I'm writing code to read in a 7x15 block of text in a file that will represent a 'maze'.

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

this was my original draft but this didnt work as it kept returning ? for each char it read.
This is the maze im testing with:

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

EDIT:
The cout is printing this:

  #############


#
############ 
 #

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

Am I not escaping \n's?

I've been coding for a few hours so I think its a simple mistake I'm not catching that's tripping me up right now. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

御弟哥哥 2024-08-14 00:10:27

尝试使用绝对路径,例如“c:\MyMazes\maze”。

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

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

 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;

Try an absolute path like "c:\MyMazes\maze".

Throw in a system("cd") to see where the current directory is. If you're having trouble finding the current directory, check out this SO discussion

Here's the complete code - this should display your entire maze (if possible) and the current directory.

 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;
反差帅 2024-08-14 00:10:27

您可以检查从文件中提取是否正确:使用 ifstreamgood() API

for(int j = 0; j < 15; j++)
{
    if(!loadMaze.good())
    {
        cout << "path incorrect";

    }

    temp= loadMaze.get();


    cout << "temp = " << temp << endl; //testing
    charBoard[i][j] = temp;
    cout << charBoard[i][j];  //testing
}

在开头本身:

ifstream loadMaze("maze"); 
if(!loadMaze.good())
{
  //ERROR
}

Can you check whether extraction from file is proper: using good() API of ifstream

for(int j = 0; j < 15; j++)
{
    if(!loadMaze.good())
    {
        cout << "path incorrect";

    }

    temp= loadMaze.get();


    cout << "temp = " << temp << endl; //testing
    charBoard[i][j] = temp;
    cout << charBoard[i][j];  //testing
}

OR

in the beginning itself:

ifstream loadMaze("maze"); 
if(!loadMaze.good())
{
  //ERROR
}
栀梦 2024-08-14 00:10:27

添加该行

if (!loadMaze) throw 1;

尝试在 loadMaze 声明之后 ,如果文件不存在,这将引发异常。这是一个 hack,你真的应该抛出一个真正的错误。但它可以测试。

try adding the line

if (!loadMaze) throw 1;

after the declaration of loadMaze, this will throw an exception if the file isn't there. This is a hack, really you should throw a real error. But it works to test.

迷路的信 2024-08-14 00:10:27

检查文件打开是否失败。您可以通过检查它是否良好来发现这一点:

http://www.cplusplus。 com/reference/iostream/ios/good/

如果打开文件失败,则尝试写入文件的绝对路径(C:/Documents and Settings/.../maze),看看是否作品。如果确实如此,则只是文件路径错误,您将不得不尝试解决该问题。

Check whether the file opening failed or not. You can find this out by checking if it's good:

http://www.cplusplus.com/reference/iostream/ios/good/

If the file opening has failed, then try writing in the absolute path to the file (C:/Documents and Settings/.../maze), to see whether that works. If it does, it's just the file path that's wrong, and you'll have to play with that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文