C++ cli 为什么可以';我不能从ifstream获取输入吗?

C++ cli 为什么可以';我不能从ifstream获取输入吗?,c++-cli,ifstream,C++ Cli,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;"; open

我正在尝试读取迷宫程序的文本文件。输入类似于:

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();
        }
     }
{
openFileDialog1->InitialDirectory=“C:\Desktop;”;
openFileDialog1->Filter=“迷宫文件(*.DAT)|*.DAT”;
如果(openFileDialog1->ShowDialog()==::DialogResult::OK)
{
字符文件名[1024];
对于(inti=0;iFileName->Length;i++)
{
filename[i]=openFileDialog1->filename[i];
}
国际单项体育联合会;
ifs.open(filename);//NULL终止此
迷宫=新迷宫(第一组,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!" );
    }
}
Maze::Maze(面板^drawingPanel、ifstream和ifs)
{
尝试
{
有效=错误;
ifs>>宽度>>高度;
内部温度=宽度;
drawingPanel->Size.Width=宽度;
drawingPanel->Size.Height=高度;
for(int i=0;i>源[j][i];//空值????
其他的
抛出'D';//我必须抛出一些东西……所以我抛出了D/*创建一个slit类,然后在那里抛出D?slit.fill(D)*/
}
//这应该是最后一次了
面板=绘图面板;
有效=真;
}
捕获(…)
{
有效=错误;
MessageBox::Show(“不是正确的迷宫文件!”);
}
}
程序运行时:ifs>>宽度>>高度未正确设置宽度和高度


我在这个网站上搜索了这个问题,没有找到任何有帮助的。对不起,我没有经验,非常感谢您的帮助 因为您使用的是Windows Form projet,所以我将为您提供一个用于读取文件的.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;
}
Maze(字符串^contentMap)
{
初始化组件();
字符串^dimension=getWords(contentMap,2);
数组^coordsString=dimension->Split(gcnew数组{''});
m_width=Convert::ToInt32(coordsString[0]);
m_height=Convert::ToInt32(coordsString[1]);
面板1->宽度=m_宽度;
面板1->高度=m_高度;
}
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;
}
String^getWords(String^input,int numWords)
{
尝试
{
int单词=numWords;
对于(int i=0;iLength;++i)
{
如果(输入[i]=''| |输入[i]='\n')
文字——;
如果(字==0)
{
返回输入->子字符串(0,i);
}
}
}
捕获(异常^ex)
{
// ...
}
返回字符串::空;
}

您的维度在完整的.Net中(私有成员m_width和m_height)。

为什么使用大的
if()
语句来决定是否读取迷宫元素?您应该检查从文件输入的结果。操作系统可能会在那里生成错误。
ifs.open(…)
成功了吗?您从不检查ifs.good()。这是C++/CLI。看在上帝的份上,别这样。如果您喜欢.NET,请使用C#。如果你喜欢C++,使用C++。但是不要使用神秘C++语法和.NET运行时缺点的组合。C++/CLI是一种供必须使用它的人使用的工具。然后这是天赐的。但是,如果你不知道百分之一百,你就是其中的一员,现在决定C或C++还不算太晚。只需用ifstream替换
cin
,这是唯一需要的更改。一旦你在迷宫中阅读,你就会知道它是好是坏。