在 ascii “迷宫”中阅读进入二维数组
我正在编写代码来读取文件中代表“迷宫”的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试使用绝对路径,例如“c:\MyMazes\maze”。
输入系统(“cd”)来查看当前目录在哪里。如果您在查找当前目录时遇到问题,请查看此 SO讨论
这是完整的代码 - 这应该显示你的整个迷宫(如果可能)和当前目录。
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.
您可以检查从文件中提取是否正确:使用
ifstream
的good()
API或
在开头本身:
Can you check whether extraction from file is proper: using
good()
API ofifstream
OR
in the beginning itself:
添加该行
尝试在 loadMaze 声明之后 ,如果文件不存在,这将引发异常。这是一个 hack,你真的应该抛出一个真正的错误。但它可以测试。
try adding the line
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.
检查文件打开是否失败。您可以通过检查它是否良好来发现这一点:
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.