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

上海交通大学交大密西根 ■■ 联合学院·一 ◆ 18T UM-SJTU Joint Institute ■ University of Michigan Shanghal Jiao Tong University Vg101:Introduction to Computer and Programming Files Records
Vg101:Introduction to Introduction to Computer and Programming Computer and Programming Files & Records

上海交通大学交大密西根 联合学院·一 ◆ 81 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Files Except for a few cases where we generated random data using quasi-random numbers, so far we have only: Input from keyboard and Output to screen This is useful for limited amounts of data. In case of a large quantity of data needs to be handled.We need functions to support input from a file and output to a file
Files • Except for a few cases where we generated random data using quasi-random numbers, so far we have only: – Input from keyboard and – Output to screen • This is useful for limited amounts of data. • In case of a large quantity of data needs to be handled. We need functions to support input from a file and output to a file

上海交通大学交大密西根 联合学院 ■ 81 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University C++'s view of files C++views a file as a sequential stream of bytes. The end of a file is determined anywhere by an end-of-file (EOF)marker,or at a specific byte count that the operating system has been told about in a program. Files are stored permanently by the operating system,usually on a hard-disk or some other medium.The operating system is responsible for the maintenance of files
C++’s view of files s view of files • C++ views a file as a sequential stream of bytes. The end of a file is determined anywhere by an end-of-file (EOF) marker, or at a specific byte count that the operating system has been told about in a program. • Files are stored permanently by the operating system, usually on a hard-disk or some other medium. The operating system is responsible for the maintenance of files

上海交通大学交大密西根 联合学院·一 ◆] 81 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University C++'s view of files The standard header files iostream (keyboard and screen-related Input/Output [I/O)and fstream (file- related 1/O)contains all the stream-file operating system dependencies and has to be rewritten for each new operating system
C++’s view of files s view of files • The standard header files iostream (keyboard and screen-related Input/Output [I/O]) and fstream (filerelated I/O) contains all the stream-file operating system dependencies and has to be rewritten for each new operating system

上海交通大学交大密西根 联合学院·一 ◆ UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Sequential Characteristics One usually uses sequential files for writing and reading data that is not changed later and that is read all at once.It is the most efficient way of storing files which have variable length records. Changing the contents of records or the numbers of records can be done,but not very efficiently. Accessing the contents of some specific records can be done,but not very efficiently
Sequential Characteristics Sequential Characteristics • One usually uses sequential files for writing and reading data that is not changed later and that is read all at once. It is the most efficient way of storing files which have variable length records. • Changing the contents of records or the numbers of records can be done, but not very efficiently. • Accessing the contents of some specific records can be done, but not very efficiently

上海交通大学交大密西根 联合学院·一 ◆ ■ 81T UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Reading from a file ·include Declaration:ifstream inFile; Open and bundle it with a physical file on a disk: inFile.open ("InputData.dat"); Compiler will look for it from the same directory as the one where the project file resides. You can also use relative or absolute path: inFile.open("C:\leng101\hw\\hw6\hw6.dat");
Reading from a file Reading from a file • #include • Declaration: ifstream inFile; • Open and bundle it with a physical file on a disk: inFile.open (“InputData.dat"); Compiler will look for it from the same directory as the one where the project file resides. • You can also use relative or absolute path: inFile.open("C:\\eng101\\hw\\hw6\\hw6.dat");

上海交通大学交大密西根 联合学院·一 ◆ 181 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Reading from a file check for successfulness: if (inFile.fail())freturn true;} Read in the input stream. string name; int score; inFile >name > score; Close when you finished:inFile.close ()
Reading from a file Reading from a file • check for successfulness: if (inFile.fail()) {return true;} • Read in the input stream. string name; int score; inFile >> name >> score; • Close when you finished: inFile.close ();

上海交通大学交大密西根 ·联合学院一 181 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Example for File Input #include #include using namespace std; int main(void) ifstream inFile;/Create an input file object char fileName[26];/1D array of chars string name; Float score; cout fileName; inFile-open(fileName);//Connect the stream to the file if (!inFile.fail()) while (!infile.eof ()infile >name >score; cout <"End of input detected\n"; inFile.close();//Disconnect the stream from the file return 8;
Example for File Input Example for File Input

上海交通大学交大密西根 联合学院·一 ◆ 81T UM-SJTU Joint Institute ■ University of Michigan Shanghal Jiao Tong University Writing to a file Follow the same procedure with read in a file except changing the ifstream inFile;to ofstream outFile; and the direction of the data as indicated by the symbols >for input to <for output!
Writing to a file Writing to a file • Follow the same procedure with read in a file except changing the ifstream inFile; to ofstream outFile; and the direction of the data as indicated by the symbols >> for input to << for output!

上海交通大学交大密西根 ·联合学院一 181t UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Example for File Output int main(void) ifstrean inFile.open ("'InputFile.txt"'); ofstream outFile.open ("OutputFile.txt"'); string name; Float score; if (!inFile.fail()&outFile.fail()) while (!inFile.eof () inFile >name >score; outFile <name <<<score <endl; }; inFile.close(); outFile.close(); }; return 0;
Example for File Output Example for File Output
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)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
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_16 MATLAB environment short.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_15 Introduction to matlab.pdf
- 上海交通大学:《数据库系统原理 The principle of Database System》教学资源_Chapter 1 Introduction.pdf
- 上海交通大学:《数据库系统原理 The principle of Database System》教学资源_第二章习题与答案(第三版).doc
- 上海交通大学:《数据库系统原理 The principle of Database System》教学资源_第三章习题与答案(第三版).doc
- 上海交通大学:《数据库系统原理 The principle of Database System》课程教学资源(课件讲稿)chapter8 Views, Indexes.pdf
- 上海交通大学:《数据库系统原理 The principle of Database System》课程教学资源(课件讲稿)Chapter7 Constraints and Triggers.pdf
- 上海交通大学:《数据库系统原理 The principle of Database System》课程教学资源(课件讲稿)Chapter6 The database Language SQL –as a tutorial.pdf
- 上海交通大学:《数据库系统原理 The principle of Database System》课程教学资源(课件讲稿)Chapter5 Algebraic and Logic Query languages.pdf
- 上海交通大学:《数据库系统原理 The principle of Database System》课程教学资源(课件讲稿)chapter4 High-level Database Models.pdf
- 上海交通大学:《数据库系统原理 The principle of Database System》课程教学资源(课件讲稿)chapter3 Design Theory for Relational Databases.pdf
- 上海交通大学:《数据库系统原理 The principle of Database System》课程教学资源(课件讲稿)chapter11 The semi-structured data model Structured data.pdf
- 上海交通大学:《数据库系统原理 The principle of Database System》课程教学资源(课件讲稿)Chapter1 Introduction.pdf
- 上海交通大学:《数据库系统原理 The principle of Database System》教学资源_intro.pdf
- 上海交通大学:《微机原理与接口技术》课程教学资源(课件讲稿)第二章 8086系统结构.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_Function.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_Introduction to Computer and Programming.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_Introduction to Vg101.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_objects and classes.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_programming style guide for C plusplus.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_Random Number_Graphics.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_Start with C plusplus.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_vector_string.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_Recitation 1.ppt
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_recitation 13.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_Recitation IX.ppt
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_Recitation V.ppt
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_Recitation VII.ppt
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_Recitation VIII.ppt
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_Recitation X.ppt
- 上海交通大学:《微机原理与接口技术》课程教学资源(课件讲稿)第十四章 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