-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