上海交通大学交大密西根 ■■ 联合学院·一 ◆ 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