2014-11-06 43 views
-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 >>高度寬度和高度未正確設置。

我已經搜索了這個網站的這個問題,一直沒能找到任何有所幫助。對不起,我的經驗不足,任何幫助,非常感謝。

+0

爲什麼大的if()語句決定是否讀取迷宮元素? – Galik 2014-11-06 19:14:30

+0

您應該檢查從文件輸入的結果。操作系統可能會產生錯誤。 – 2014-11-06 19:14:54

+0

'ifs.open(...)'成功了嗎?你從不檢查'ifs.good()'。 – Barry 2014-11-06 19:15:22

回答

1

You'e程序非常難看:不知道你是否在C或C++編程或C++/CLI,或嘗試混合3 ...

由於您使用的Windows窗體謨,我會給你一個.Net解決方案來讀取文件,這不是更好的解決方案,但這不會混合的東西。

首先用於讀取文件,第一窗口上:

private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) 
{ 
    openFileDialog1->Filter = "Maze Files (*.dat) | *.dat"; 
    if (openFileDialog1->ShowDialog() == ::DialogResult::OK) 
    { 
     String ^fileName = openFileDialog1->FileName; 
     IO::StreamReader ^myMazeFile = gcnew IO::StreamReader(fileName); 
     String ^content = myMazeFile->ReadToEnd(); 
     richTextBox1->Text = content; 
     myMazeFile->Close(); 
     // display button for open second form wich draw maze 
     button2->Visible = true; 
    } 
} 

現在我們有我們的文件的內容,所以我們把它傳遞給第二個表格誰將會畫出迷宮:

private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) 
{ 
    String ^content = richTextBox1->Text; 
    Maze ^frm = gcnew Maze(content); 
    frm->Show(); 
} 

第二個窗口,創建超載構造:

Maze(String ^contentMap) 
{ 
    InitializeComponent(); 

    String ^dimension = getWords(contentMap, 2); 
    array<String ^> ^coordsString = dimension->Split(gcnew array<Char> {' '}); 
    m_width = Convert::ToInt32(coordsString[0]); 
    m_height = Convert::ToInt32(coordsString[1]); 
    panel1->Width = m_width; 
    panel1->Height = m_height; 
} 

getWords方法:

String ^getWords(String ^input, int numWords) 
{ 
    try 
    { 
     int words = numWords; 
     for (int i = 0; i < input->Length; ++i) 
     { 
      if (input[i] == ' ' ||input[i] == '\n') 
       words--; 
      if (words == 0) 
      { 
       return input->Substring(0, i); 
      } 
     } 
    } 
    catch (Exception ^ex) 
    { 
     // ... 
    } 
    return String::Empty; 
} 

您有完整的.Net(私有成員m_width和m_height)的維度。

相關問題