Computer Programming II Lecture 9 Files Processing - We have been using the iostream standard library, which provides cin and cout methods for reading from standard input and writing to standard output respectively. - In this lecture we will know how to read and write from a file. Files Processing - A file is a collection of information, usually stored on a computer’s disk. Information can be saved to files and later retrieved. - The two basic types of files are : text files and binary files. Files Processing Types of File Access: - Sequential access: With this type of file access one must read the data in order, much like with a tape, whether the data is really stored on tape or not. - Random access (or direct access): This type of file access lets you jump to any location in the file, then to any other, etc., all in a reasonable amount of time. Files Processing C++ File I/O Classes : There are 3 File I/O classes in C++ which are used for File Input(Read) / Output (Write) operations : • ifstream : It is used to read information from file. • ofstream : It is used to create files and to write information to file. • fstream : It has the capabilities of both ofstream and ifstream which means it can create files, write information to file, and read information from file. *To perform file processing in C++, header files <iostream> and <fstream> must be included in your C++ source file. Files Processing Opening a file: A file must be opened before you can read from it or write to it. Either the ofstream or fstream object may be used to open a file for writing and ifstream object is used to open a file for reading purpose only. Files Processing - In order to open a file with a stream object we use its member function open(): object_name.open (filename, mode); - Where filename is representing the name of the file to be opened, and mode is an optional parameter with a combination of the following flags: Files Processing Files Processing Writing a file : While doing C++ programming, you write information to a file from your program using the stream insertion operator (<<) just as you use that operator to output information to the screen. The only difference is that you use an ofstream or fstream object instead of the cout object. Files Processing Writing to a text file using fstream class: Writing to a text file can be achieved with the stream operators. This also follows the same order of operations, though with a slight difference. 1. open a file – in write mode. 2. Write to a file. 3. close the file. - This sample code snippet explains how to use the C++ file i/o stream operators to write data from a file. In all cases the header file fstream.h must be included. Files Processing Files Processing Reading from a file: You read information from a file into your program using the stream extraction operator (>>) just as you use that operator to input information from the keyboard. The only difference is that you use an ifstream or fstream object instead of the cin object. Files Processing Reading a text file using (fstream) class: There are several ways of reading the text from a file. But all of them have a common approach as follows: • Open the file • Read the data • Close the file Look at the following sample code to see the difference. Files Processing Files Processing Closing a file: When we are finished with our input and output operations on a file we shall close it so that its resources become available again. In order to do that we have to call the stream's member function close(). This member function takes no parameters, and what it does is to flush the associated buffers and close the file: Object_name.close( ); Files Processing - Write a C++ program that used to: 1- Allow user to enter his\her name. 2- Create a text file with this name (myfile) on the disk drive (D) and then save the user’s name inside it. Files Processing Files Processing Files Processing - Write a C++ program that used to create a text file with this name (student) on the disk drive (D) and then save the data for 3 students inside it. Note : - The student data are : student number and student name. - These data should inserted by user. Files Processing Files Processing Files Processing - Write a C++ program that used to allow you to enter the student data and then it will add it to the previous text file which named as (student) on the disk drive (D). Note : - The student data are : student number and student name. Files Processing Files Processing