上海交通大学交大密西根 联合学院 81 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Open Files Safely for Writing outFile.open ("OutputFile.txt")will destroy the OutputFile.txt if it is already existed in the directory it tries to open. In order to avoid this,you should first open it for reading.then test to see if the file open fails. If it fails,it means thatthat file does not exist and it is safe to write to that filename.If the open does not fail,it means that the file exists.In this case you should prompt the user to see if the file should be overwritten
Open Files Safely for Writing Open Files Safely for Writing • outFile.open (“OutputFile.txt”) will destroy the OutputFile.txt if it is already existed in the directory it tries to open. • In order to avoid this, you should first open it for reading. then test to see if the file open fails. If it fails, it means that that file does not exist and it is safe to write to that filename. If the open does not fail, it means that the file exists. In this case you should prompt the user to see if the file should be overwritten
上海交通大学交大密西根 联合学院 181 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Caution:File Names in C++ For historical reasons,the 1O library uses C-style character strings rather than C++ strings for a file names.When we call open or use a file name as the initializer when creating an fstream object,the argument we pass is a C-style string,not a library string. We can obtain a C-style string from a C++ string via: str.c_str (
Caution: File Names in C++ Caution: File Names in C++ • For historical reasons, the IO library uses C-style character strings rather than C++ strings for a file names. When we call open or use a file name as the initializer when creating an fstream object, the argument we pass is a C-style string, not a library string. • We can obtain a C-style string from a C++ string via: str.c_str ()
上海交通大学交大密西根 ·联合学院一 181 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Example ifstream inFile;/Create an output file object ofstream outFile;/Create an output file object int overWriteIt; cout <"What filename do you want to write to:" string fileName;/1D array of chars cin >fileName; inFile.open(fileName.c_str()); if (!inFile.fail()) cout <"File called "<fileName <already exists.\n"; cout <"Overwrite (Input 0 for no,any other int to overwrite):" cin >overWriteIt; if (!overWriteIt)return 0; inFile.close(); outFile.open(fileName.c_str());//Connect the stream to the file
Example Example
上海交通大学交大密西根 联合学院 ◆ 81 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Key Points about Files It's a sequential binary stream.It is designed for sequential writing and reading data. To read/write a file you need 6 steps -#include <fstream> Declaration:ifstream inFile (ofstream outFile); 18 -Open and bundle it with a physical file on a disk Check for successfulness with in File/outFile.fail Read in the input stream/Write to the output stream. Close when you finished:inFile/outFile.close ()
Key Points about Files Key Points about Files • It’s a sequential binary stream. It is designed for sequential writing and reading data. • To read/write a file you need 6 steps – #include <fstream> – Declaration: ifstream inFile (ofstream outFile); – Open and bundle it with a physical file on a disk – Check for successfulness with inFile/outFile.fail () – Read in the input stream/Write to the output stream. – Close when you finished: inFile/outFile.close ();
上海交通大学交大密西根 联合学院· ■ 81T UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Designing a Database In this class,we will show you how to design a data base.To get some ideas on what is involved in doing that,we will look at a detailed example. Example of the class have all the code for the final result,and Handout schoolDB.pdf is a discussion of the design that will complement what is said in lecture. The example we will discuss is a database for a small college
Designing a Database Designing a Database • In this class, we will show you how to design a data base. To get some ideas on what is involved in doing that, we will look at a detailed example. Example of the class have all the code for the final result, and Handout schoolDB.pdf is a discussion of the design that will complement what is said in lecture. • The example we will discuss is a database for a small college