c++ cli - Why can't I get input from the ifstream? -
i trying read in text file maze program. input like:
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
when user click on open button, opens open file dialog box
{ openfiledialog1->initialdirectory = "c:\desktop;"; openfiledialog1->filter = "maze files (*.dat)|*.dat"; if (openfiledialog1->showdialog() == ::dialogresult::ok) { char filename[1024]; (int = 0; < openfiledialog1->filename->length; i++) { filename[i] = openfiledialog1->filename[i]; } ifstream ifs; ifs.open(filename); // null terminate maze = new maze( panel1, ifs); ifs.close(); } }
the next maze constructor
maze::maze( panel ^ drawingpanel, ifstream & ifs ) { seek { valid = false; ifs >> width >> height; int temp = width; drawingpanel->size.width = width; drawingpanel->size.height = height; (int = 0; < height; i++) // height nil (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'; // had throw something....so threw d /* create slit class , throw d there? slit.fill(d); */ } // should lastly panel = drawingpanel; valid = true; } grab (...) { valid = false; messagebox::show( "not proper maze file!" ); } }
when programme runs: ifs >> width >> height width , height not set correctly.
i have searched site problem , have not been able find has helped. sorry inexperience, help appreciated.
you'e programme ugly : don't know if you're programming in c or c++ or c++/cli, or seek mix 3...
because utilize windows form projet, give .net solution read file, it's not improve solution not mix things.
first read file, on first window :
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 open sec form wich draw maze button2->visible = true; } }
now have our file content, pass sec form draw maze :
private: system::void button2_click(system::object^ sender, system::eventargs^ e) { string ^content = richtextbox1->text; maze ^frm = gcnew maze(content); frm->show(); }
second window, create overload constructor :
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 method :
string ^getwords(string ^input, int numwords) { seek { int words = numwords; (int = 0; < input->length; ++i) { if (input[i] == ' ' ||input[i] == '\n') words--; if (words == 0) { homecoming input->substring(0, i); } } } grab (exception ^ex) { // ... } homecoming string::empty; }
you have dimension in total .net (private fellow member m_width , m_height).
c++-cli ifstream
No comments:
Post a Comment