-1
我想读一个迷宫程序的文本文件。输入是这样的:为什么我无法从ifstream获得输入?
10 10
OO+E+OO+++
O++O+O+OOO
OOOOOO+O+O
+++++O++OO
OOO+OOO+O+
O+O+O+++O+
O+O+OOO+OO
++O+++O++O
O+OOOOO++O
O+O++O+OOO
当打开按钮,用户点击,这将打开一个文件打开对话框
{
openFileDialog1->InitialDirectory = "C:\Desktop;";
openFileDialog1->Filter = "Maze files (*.DAT)|*.DAT";
if (openFileDialog1->ShowDialog() == ::DialogResult::OK)
{
char filename[1024];
for (int i = 0; i < openFileDialog1->FileName->Length; i++)
{
filename[i] = openFileDialog1->FileName[i];
}
ifstream ifs;
ifs.open(filename); // NULL terminate this
maze = new Maze(panel1, ifs);
ifs.close();
}
}
下面是迷宫构造
Maze::Maze(Panel^drawingPanel, ifstream & ifs)
{
try
{
valid = false;
ifs >> width >> height;
int temp = width;
drawingPanel->Size.Width = width;
drawingPanel->Size.Height = height;
for (int i = 0; i < height; i++) // height is always nothing
for (int j = 0; j < width; j++)
{
if (orig[j][i] == DEADEND ||
orig[j][i] == OPEN ||
orig[j][i] == EXIT)
ifs >> orig[j][i]; // NULLS????
else
throw 'D'; // i had to throw something....so i threw the D /* make a slit class and throw the D there? slit.fill(D); */
}
// this should be last
panel = drawingPanel;
valid = true;
}
catch (...)
{
valid = false;
MessageBox::Show("Not a proper maze file!");
}
}
时该程序运行:ifs >> width >>高度宽度和高度未正确设置。
我已经搜索了这个网站的这个问题,一直没能找到任何有所帮助。对不起,我的经验不足,任何帮助,非常感谢。
为什么大的if()语句决定是否读取迷宫元素? – Galik 2014-11-06 19:14:30
您应该检查从文件输入的结果。操作系统可能会产生错误。 – 2014-11-06 19:14:54
'ifs.open(...)'成功了吗?你从不检查'ifs.good()'。 – Barry 2014-11-06 19:15:22