上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_Recitation X

VG101 RECITATION 10 By TAs
VG101 RECITATION 10 By TAs

CONTENTS o File I/O o Pointer in database design o About final exam Q&A
CONTENTS File I/O Pointer in database design About final exam Q & A

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

FILE I/O 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
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

FILE I/O o To use file modes: infile.open(fileName.c_strO,ios::in); outfile.open(fileName.c_str0,ios::app); o You'll need ios::app in most cases if you don't want to overwrite your file data. o For more file modes or useful functions,look up ICP book,chapter 12,or use zhidao.baidu.com
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

FILE I/O o Check before using any file stream object o For input: 。if(infile.failO)∥handle this exception o For output: ·ifstream infile; ●ofstream outfile; .infile.open(fileName.c_strO); ·if(!infile.fail0) 。{ cout <"File "<fileName <already exists\n"; cout <<"Do you want to overwrite it?\n"; ·/handle user's reply ·}
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 }

FILE I/O o Alternative way for output check,using file modes: o If you want to append the new data to the end of the original file: outfile.open(fileName.c_str0,ios::app); ·if(outfile.failO)∥handle this exception o If you want to erase the old data in the file and write the new data into it: outfile.open(fileName.c_strO); 。if(outfile..failO)l∥handle this exception
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

FILE I/O o String and char*: o Some functions in C++are written in C,so these functions don't recognize string class. oUse“.c_str0”to convert a string to char* infile.open(fileName.c_str0,ios::in); o You need to include if you want to use this function as well as many other string functions
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 if you want to use this function as well as many other string functions

POINTER IN DATABASE DESIGN o Why we use pointer: o 1.Pointer is just a variable of an address,it points to an space in memery,so it's space saving. o 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
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

POINTER IN DATABASE DESIGN o 3.If we change teacher A through pointer in course B,when we look through pointer in course C,teacher A is also changed o 4.Using pointer allows us to dynamically add new objects by“new”keyword o 5.Pointer form a web of relationship between unique objects,just as the real world
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
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_Recitation VIII.ppt
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_Recitation VII.ppt
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_Recitation V.ppt
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_Recitation IX.ppt
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_recitation 13.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_Recitation 1.ppt
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_vector_string.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_Start with C plusplus.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_Random Number_Graphics.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_programming style guide for C plusplus.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_objects and classes.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_Introduction to Vg101.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_Introduction to Computer and Programming.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_Function.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_files_DataBase Design.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_Expressions and Statements.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_examples on class design.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_Array and its Applications.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_20 Looking Ahead.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_19 Recursion 1.pdf
- 上海交通大学:《微机原理与接口技术》课程教学资源(课件讲稿)第十四章 MCS-51单片机(1/2).pdf
- 上海交通大学:《微机原理与接口技术》课程教学资源(课件讲稿)第十四章 MCS-51单片机(2/2).pdf
- 上海交通大学:《微机原理与接口技术》课程教学资源(课件讲稿)第一章 微机原理与接口技术绪论(朱兰娟).pdf
- 上海交通大学:《微机原理与接口技术》课程教学资源(课件讲稿)第二章 8086系统结构.pdf
- 上海交通大学:《数据库系统原理 The principle of Database System》课程教学资源(课件讲稿)chapter9 SQL in a server environment.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)05年期末习题_Pointer Review Solution.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)05年期末习题_Pointer Review.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)05年期末习题_Practice Final 1.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)05年期末习题_practice Final 2.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)05年期末习题_practice Final 3.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)05年期末习题_Solution for Practice Final 1.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)05年期末习题_Solution for Practice Final 2.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)C++语言_I.运算符重载.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)C++语言_II.继承与派生.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)C++语言_III.多态性.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)C++语言_IV.异常处理与模板.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)C++语言_V. 流库.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)C++语言_VI.类与对象.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)C++语言_VII.类与对象的高级操作.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)C语言_I.C语言概述.pdf