1 / 13

VG101 RECITATION 10

VG101 RECITATION 10. By TAs. CONTENTS. File I/O Pointer in database design About final exam Q & A. FILE I/O. #include <iostream> #include <fstream> #include <string> using namespace std;. void main() { ifstream infile; string fileName; cin >> fileName;

Télécharger la présentation

VG101 RECITATION 10

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. VG101RECITATION 10 By TAs

  2. CONTENTS • File I/O • Pointer in database design • About final exam • Q & A

  3. FILE I/O #include <iostream> #include <fstream> #include <string> using namespace std; • void main() • { • ifstream infile; • string fileName; • cin >> fileName; • infile.open(fileName.c_str(), ios::in); • if(infile.fail()) • { • cout << "File " << fileName << " don't exist\n"; • return; • } • else • { • cout << "Open file " << fileName << " success\n"; • } • } File modes To use .c_str(), you need to include this Check before using

  4. FILE I/O • File modes: • ios::in: opens a file for input • ios::out: opens a file for output • ios::app: appends all output to the end of the file • ios::ate: opens a file for output. If the file already exists, move to the end of the file. • ios::truct: discards the file’s contents if the file already exists. (default action for ios::out) • ios::binary: opens a file for binary input and output

  5. FILE I/O • To use file modes: infile.open(fileName.c_str(), ios::in); outfile.open(fileName.c_str(), ios::app); • You’ll need ios::app in most cases if you don’t want to overwrite your file data. • For more file modes or useful functions, look up ICP book, chapter 12, or use zhidao.baidu.com

  6. FILE I/O • Check before using any file stream object • For input: • if(infile.fail()) // handle this exception • For output: • ifstream infile; • ofstream outfile; • infile.open(fileName.c_str()); • if( ! infile.fail() ) • { • cout << "File " << fileName << " already exists\n"; • cout << "Do you want to overwrite it?\n"; • // handle user's reply • }

  7. FILE I/O • Alternative way for output check, using file modes: • If you want to append the new data to the end of the original file: • outfile.open(fileName.c_str(), ios::app); • if(outfile.fail()) // handle this exception • If you want to erase the old data in the file and write the new data into it: • outfile.open(fileName.c_str()); • if(outfile.fail()) // handle this exception

  8. FILE I/O • String and char*: • Some functions in C++ are written in C, so these functions don’t recognize string class. • Use “ .c_str() ” to convert a string to char* infile.open(fileName.c_str(), ios::in); • You need to include <string> if you want to use this function as well as many other string functions

  9. POINTER IN DATABASE DESIGN • Why we use pointer: • 1. Pointer is just a variable of an address, it points to an space in memery, so it’s space saving. • 2. A teacher may teach several courses, a classroom may be used by several courses. By using pointer, an object can be referenced by N other objects without creating N objects.

  10. POINTER IN DATABASE DESIGN • 3. If we change teacher A through pointer in course B, when we look through pointer in course C, teacher A is also changed • 4. Using pointer allows us to dynamically add new objects by “new” keyword • 5. Pointer form a web of relationship between unique objects, just as the real world

  11. POINTER IN DATABASE DESIGN • Where we use pointer and where we don’t: • Notice that we use pointer in class DepartmentT and SchoolT, but we don’t do that in class LocationT and InstructorT • School and department is a relationship between instructors, students, classrooms…while instructor is a real object. • Pointer is a tool to handle relationship or reference

  12. ABOUT FINAL EXAM • All materials will be covered • Focus on skills in programming, not on basic concept • Computer part + paper part • Programming style will be graded

  13. Q & A • Any questions regarding • File I/O • Pointer • Exam

More Related