1 / 12

Scientific Programming WS 06/07 C++ / WIN Olaf Kolditz W Wang

Scientific Programming WS 06/07 C++ / WIN Olaf Kolditz W Wang. http://www.uni-tuebingen.de/zag/teaching/scientific_programing/index.htm. Programm GUI L7 30.01.2007.

Download Presentation

Scientific Programming WS 06/07 C++ / WIN Olaf Kolditz W Wang

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Scientific Programming WS 06/07 C++ / WIN Olaf Kolditz W Wang http://www.uni-tuebingen.de/zag/teaching/scientific_programing/index.htm Scientific Programming II / C++

  2. Programm GUIL730.01.2007 Scientific Programming II / C++

  3. Open the Visual Studio 6 developer program.Create a new project as MFC-Application (.exe) in the Desktop Folderwith the project name „E7_dialog“Set the application type to Dialog basedSkip the two next steps without changes and finish the project setupGo to the desktop and find the new created project folder withan empty Debug folder inside.Compile the project and find the E7_dialog.exe file in the DebugfolderGo in the Visual Studio 6 program to the Ressource tab, openthe dialog resource in the Dialog folder and add a button to thedialog.Change the ID of the new button with right mouse click and propertiesto „IDC_TESTBUTTON“ and the caption to „Test“ Task 1: Dialog based MFC-Application Scientific Programming II / C++

  4. Click with the right mouse button to the new button and selectEvents.. from the context menuSelect BN_CLICKED as Event and the right ID of the buttonand add an EventHandler-Function with the name „OnTestbutton“Go to the Sources tab, open the sourcefile „E7_dialogDlg.cpp“ andfind the entry for the new event in the Message Map sector(BEGIN_MESSAGE_MAP)Go to the function code for the EventHandler-Function „OnTestbutton“which is still empty.Add the following code to the function:MessageBox(„Testbutton works!“);Compile and run the project an test the „Test“-Button. Task 1: Dialog based MFC-Application Scientific Programming II / C++

  5. Download the project C++_E7_GUI_02.zip unpack and open it.Open the Dialog Ressource „IDD_MYFIRSTWINDOWS-DIALOG“ andget the ID of the „Write DB“ button.Go to the Message Map and get the name of the EventHandler-funktionfor the „Write DB“ button.Go to the EventHandler-funktion and add a code for writingthe contend of the vector student_vector instead of the line//Add new code here!!There is a member function in the CStudent class (Print()), whichcan be used to write to an ofstream object!!Write the different elements of the student_vector by usingthe Print() function in a loop asfor(int i=0;i<(int)student_vector.size();i++) { ...... } Task 2: Add code to OnButtonWriteDB() funktion Scientific Programming II / C++

  6. More on I/O by using iostream Scientific Programming II / C++

  7. STL Stream class • C++ provides the following classes to perform output and input of characters to/from files: • ofstream: Stream class to write on files • ifstream: Stream class to read from files • fstream: Stream class to both read and write from/to files. • Accocated header files: • #include <iostream> // for using cout • #include <fstream> // for using streams Scientific Programming II / C++

  8. STL Open a file open (filename, mode); or call one constructor of the stream with arguments (filename, mode) Mode: Scientific Programming II / C++

  9. STL Open a file • Example: • ifstream my_in(„data.txt“, ios::in); • // ifstream *my_in = new ifstream(„data.txt“, ios::in); • alternatively: • ifstream my_in; • my_in.open („data.txt“, ios::in); • ofstream my_out(„data.txt“, ios::out|ios::trunc); • //ofstream *my_out = new ofstream („data.txt“, ios::out|ios::trunc); • alternatively: • ofstream my_out; • my_out.open („data.txt“, ios::out|ios::trunc); Scientific Programming II / C++

  10. STL Locate the position of the pointer in stream pos_type tellg(); istream& seekg( off_type off, ios::seekdir origin ); istream& seekg( pos_type pos ); Parameters pos The new position in the stream buffer. off Integral value of type streamoff representing the offset dir Seeking direction. ios_base::beg beginning of the stream buffer ios_base::cur current position in the stream buffer ios_base::end end of the stream buffer Scientific Programming II / C++

  11. STL Locate the position of the pointer in stream Example: ios::pos_typeposition; ifstream my_in(„data.txt“, ios::in); .... position = my_in.tellg(); .... my_in.seekg(position,ios::beg); .... #STUDENT $NAME_FIRST James $NAME_LAST Bond $COURSE MI6 $YEAR 1963 $BANK_ACCOUNT 007007007 #STUDENT1 $NAME_FIRST James1 $NAME_LAST Bond1 $COURSE MI6 $YEAR 1973 $BANK_ACCOUNT 008008008 Scientific Programming II / C++

  12. STL Example in the excercise char line_char[MAX_LINE]; string line_string; ios::pos_type position;// Used record the position in fstream while(!data_set_file.eof()) { data_set_file.getline(line_char,MAX_LINE); line_string = line_char; if(line_string.find("#STOP")!=string::npos) return; //.................................................................... if(line_string.find("#STUDENT")!=string::npos){// keyword found CStudent* m_std =new CStudent(); position = m_std->ReadDataSet(data_set_file); data_set_file.seekg(position,ios::beg); student_vector.push_back(m_std);// Insert element to vector }// keyword found }// eof Scientific Programming II / C++

More Related