 Welcome to this session on using files in our C++ programs. Last time we saw a brief introduction on how the data is externally stored on storage devices such as magnetic desk or CDs or pen rides. Today we wish to look at how exactly we could read such data in our programs or how we could create an output file and write the desired output data on to an external file. Here is the basic block diagram of what we wish to do. We have our program here. We wish this program to read data from a file. As an example I have taken a file which is known as participants.txt which is let us say an external text file stored on the disk and it contains data about names of participants, the marks scored by them in an exam etc etc. What I wish to do is to read this data into my program, calculate some statistics such as average marks scored by the participants, the standard deviation and instead of producing the output on my terminal I wish to write the text output in a file called statistics.txt. Which I wish to write on to a desk. In this session we are going to look at the facilities available in C++ to handle such external data. To begin with inside a C++ program a file is declared as a special pointer called a file pointer. We can choose any name for this pointer. These names have nothing to do with the external file names but these are the internal names within the C++ program. For example I could declare file star in file. From this point onwards in file becomes an internal file name for the file from which ostensibly I wish to read data into my programs. Similarly I could declare for example a file called out file. Clearly these names infile and out file are arbitrary names of my choice and they have no connection whatsoever with any external real file. So the first thing that we need to do in a program is to establish an association with an internal file name and a corresponding external name. There are a whole lot of library functions provided in C++. These actually come from a library called CSTDIO or the standard IO library of the type C. That is where the basic file operations were first defined. They are all available in C++ as well. Of course when we study the object oriented approach of programming we will see minor deviations in the way in which such functions have to be used. But basically whatever we are going to discuss applies to all of file handling. Coming back to our discussion having declared such file pointers in our program as I mentioned we have to have an association with an internal file name with an external file. Here for example we want to read data from the file called participants.text. For that there is a special function called fopen. As the name suggests we are opening an external file. This fopen function requires two parameters. The first parameter is the external file name and the second parameter is called a mode value which is actually a text symbol indicating in what fashion we wish to use the data. For example I could open the file in an input mode in which case I read data. I could open the file in an output mode in which case I write data. I could have an ordinary text file as the name suggests here or they could be special files such as digital image files or any other type of file which contains purely binary data. For the purposes of processing data from this file for example we could use the fopen statement in the following fashion. So notice what we are doing we are invoking a function fopen inside it we are giving the name of an external file as known to the operating system and then we are giving a mode which is called the read mode. Clearly the file is being opened for input operations. This function will return a pointer and that pointer we wish to associate with our own internal file pointer called infile which was declared here. There are two possibilities one of course the program with the help of the operating system is able to locate this file and open it for reading operations. But there is a possibility that the file does not exist maybe we have misspelled the name in which case what will be created by fopen as a pointer what pointer will get assigned to infile. The simple answer by C++ is in case the file can be correctly opened a pointer a valid pointer is associated to infile. In case fopen is unable to locate the file or for some reason not able to open it for your operations it will return a null pointer that means infile will be a null pointer. We can actually do that testing by saying if infile equal equal null that means I was not able to open the file. I can simply output a message for example I can say file does not exist and give this message on my terminal and then return minus 1 is the typical way in which the opening of a file is handled. You can easily guess that in exactly the same fashion an f close could be handled when you close the file that is when you have finished reading or writing from that file. If you want to open a file for writing such as out file in which you want to write something out here you will use exactly similar f open statement the only difference will be that in that case you will not use R as the mode but you will use W as the mode. So for example let me show you a how an output file could be opened you will recall we have declared a file such as file star out file. In our program if we say out file is equal to f open you see it is exactly the same way that you opened a file for input the only difference is that in the mode you specify the later W to indicate that you want to write the data into this file that means it is an output file. When such an f open statement is issued by your program the program will actually with the help of operating system will try to create a file statistics not txt as per the name that you have given. Ordinarily you should succeed in creating such a file in which case the program will return a valid pointer for out file but it is possible for example that you do not have permission to open such a file in the sub directory in which your current execution is going on or there may be no space on the disk for any reason if the operating system is not able to open this file that means it is not able to create this file it will return again a null pointer which you can check. So once again you can have statements like if out file is equal to null and then you can create an output message and you can say return minus 1 for example to indicate that the operation of opening an out file has not succeeded. With this we have now understood how we can associate external files with internal file pointers to recapitulate we use the f open statement to associate an actual file with our program pointer and we indicate the mode in which the file is to be opened which could be either R for reading and W for writing. There are additional indicators that can be given in this mode all of these are described in a separate handout which is included in the course material thank you.