2014-11-06 125 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)的维度。

相关问题