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 <iostream> #include <fstream> #include <string> 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 << "File " << fileName << " don't exist\n"; return; } else { cout << "Open file " << fileName << " success\n"; } } #include <iostream> #include <fstream> #include <string> 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