 Welcome to this course on computer programming. In this session, we will study how to access and process records directly in a binary file. You will remember that last time we wrote a program to create a binary file which contained fixed length records. Each record contained data for one studio. There were four fields in each record, roll number, name, batch and marks. What we wish to do in this session is to see which C++ functions are to be used for directly accessing and updating records for students. First, we will look at some basic aspects of accessing records in a binary file. Binary file or like any other file is a sequence of bytes and there is a file pointer F P associated in the file name. Now, please note that an internal position indicator is maintained by C++ for each file. Any reading or writing actually occurs only at this byte position and whenever a reading or writing occurs, then this indicator is automatically advanced by the number of bytes read or written. So, this is a feature of C++ file processing. Now, this position indicator is normally hidden from us, but for binary files it is possible to A, find out what is this position indicator and B, it is also possible to set this position indicator at a specified byte. In fact, these two features permit us direct access to records in a binary file. For example, to find out the current position of the position indicator, we can use the function F tell. It is like tell me where the position is. So, if I say F tell F P and assign it to a long int pause, then pause will now have the value of the position indicator which currently the position indicator is positioned. If I want to read or write at this position is okay, but suppose I want to read or write at some other byte position P, then I can use a function F seek. It is like seeking a position in the file. Please note the parameters F P is the file name, P is the byte number to which we are seeking. Seek underscore set is merely a parameter which indicates that the number of bytes or the displacement is to be counted from the beginning. In effect, P will be the number of bytes of the file at which point I would position the indicator. There are two or three other choices, but this is the most important and most frequently used choice. So, to recapitulate A, we can find out the current position of the position indicator by F tell and B, we can set the current position indicator at any place that we desire by using F seek. Once we do that, the two functions F read and F write can be used to read or write data. Please note both will operate at the current position indicator only. F read will read as many bytes as are specified by the record size and number of records from the file F P into the structure variable S whose pointer is passed. Exactly in the same way F write will write the contents of a structure variable whose pointer is passed to an output file F P. What it will do is from this point it will write as many records as we say here and it will write exactly the rake underscore size of number of bytes. It is possible that we may wish to reset the internal pointer to beginning of the file. There is a special function called rewind. It is like rewinding a tape to bring it to the beginning position of the file. Equipped with this, we can now write a program to process student DB. This is merely a program for illustration. So, I have used this opportunity to illustrate two features. One, how can I search for a particular student whose roll number is given sequentially? Please note that although the file is a binary file and permits direct access, I can always process the records one after another sequentially. So, we will do that and we will also see how we can directly access a desired record. So, let us see what we do in this program. We define the struct student info as before. We define a student info type variable S. These are the internal variables that we use. Rake size is the size of struct info and we know that this is the record size which would be read or which would be written. We have a single file to process the same file from which we will read data and the same file to which we will write updated data records or additional data records. So, we use a spatial parameter in the F open state. Name of the file is student DB of course, but we open as RB plus. It means ordinarily it is open for reading, but I can also write to it. I can also append to it. This is a binary file represented by B. As usual if the file pointer return is null, I print an error message and get out. Otherwise I start with let us say variable found equal to 0 and count equal to 0. Notice what do I do? I first rewind Fp. Now, I am searching sequentially for marks of a roll number 10105. So, what do I do? I set R equal to 10105, set found to 0 and exactly like I would have searched in a sequential text file, I start looking at records one after another in the binary file. I also note the current file position by using F tell so that I can print it while when I find the desired number. The problem is to find marks for the roll number 10105. So, let us see what we do. I set up a do while loop by this type. So, what do I do in the do while loop? I first read one record in the structure pointer S. Once I have got S, I have got S dot roll, S dot marks, S dot batch, S dot name everything. So, I simply check if S dot roll is equal to the given R. If it is then I have found the student I will print the record. So, all this is print this record. It is found at position divided by rake underscore size plus 1. So, this is the record number. I have output the message that if the record is found at this record number and I print the name and marks of that student and I have finished. So, I need to get out of this do while loop. So, in case I find a record where the roll number matches, I simply print that information out otherwise what do I do if that is not so else. I again recalculate the pause which will be the current position which will be the next position after that record and I read the next record and go back for the while loop. What is the condition of the while loop? While not end of file of FB. Notice that I could have used the while loop I am using do while loop here, but both serve exactly the same purpose. If at the end the roll number was not there in the file at all then I will know it because found will still remain zero and if found is zero I will say roll number not found in the database. Next I try to demonstrate how direct access to records can be done. So, what I am trying to do is read and display the sixth record in the file. This is a arbitrary number sixth record. So, how do I go directly to the sixth record? I set rake num as 6 and then I calculate the position of this record which is equal to rake num minus 1 into rake sign. So, this is the place where the sixth record will begin. Now, I use the F6 statement to set the internal file position pointer. F6, FB, pause, seek set. So, this will set the internal pointer to the beginning of the sixth record and when I issue a F read command it will read actually the sixth record. One record of rake size will be read into the pointer into the structure variable s whose pointer is passed. I just print out that just for verification I print the roll number name batch and marks and I say record number is rake num. This will verify whether I have correctly read the stipulated record or not. Here is another example of updating the record of a particular student. For example, I wish to update Neil Money-Rouch marks to 93.5. You will recall his marks when 91.5. Now, his roll number is 10108. We access eighth record starting at 7 into rake sign. How do we understand this? Well, roll number is 10108. We presume that all roll numbers were serial 10101, 10102, 10103, etcetera, etcetera. In fact towards the end the file has some missing roll numbers, but if we assume that database has been created for all roll numbers then it is easily possible to figure out the record for a given roll number. What do we do? We say record number is simply r minus 10101. In short this is nothing but a mapping between the roll number and a record number. Once such a mapping is available for any roll number we can calculate the record number, calculate the position of the starting byte of that record number by simply saying pos equal to rake num minus 1 into rake sign. Now, we do an FC which will directly take the internal file pointer to the desired position pass and once we go there we simply read a record. This will be the desired record in fact. We print out the record of Neil Money, so and so and this is the detailed information. But this is about reading. We want to change his marks from 91.5 to 93.5. So, what we do? We have the values of Neil Money Rao's record in structure variable s. We simply set s dot marks equal to 93.5. All other elements remain the same value. Now, I wish to rewrite this record in the same position, but please note that the actual act of reading the Neil Money Rao's record has advanced the internal position indicator. So, we need to bring it back. We simply FC again to the same pass which was issued when we wanted to read this record. We know that at this point Neil Money Rao's record begins. So, file pointer is actually brought back to the beginning of this record. Now, when I write actually the old record is rewritten with the new values in the structure variable s. Note that the s dot marks is now 93.5. So, it is this value which will be written. Are we sure that a new value has been written? Well, it is simple to verify whether correct data is written or not. I again do an FC. Please note F write will also advance the internal file pointer by one record. So, we bring it back by using FC again read this record. So, we first read a record, updated it and again read it to verify. Now, when I output the updated record, I will know whether the updation has happened correctly or not. At the end of course, I close this file and return with a zero. Let us look at the execution results just to understand how the program has progressed. It trains size of each record is 44, then it is searching sequentially for marks of roll number 10105. Through the iteration that we had set up, it found roll number 10105 at record number 4 and the name and marks are Nandan 67.00. This number is written as 4, because the actual internal number starts with 0, otherwise actually a fifth. Next, we were demonstrating direct access to records. So, we want to read and display sixth record. The record starting at byte position 220. Please note this 220 is 6 minus 1 into 44, which was the record. So, this is the starting byte for the record number 6 and this is the record 10106. Avinash this, this, this. Next most important, we want to read a record, update its values and rewrite it. So, read and update Neil Money Routes marks. Neil Money Routes roll number is 10108. So, record for Neil Money is 10108 Neil Money 11191.50. Now, updated record in database file is this. So, we confirm that earlier value was 91.5, new value is 93.5. In summary, we have seen that records in binary file can be written and accessed and processed directly. We typically search on a key attribute such as roll number. What is important is we need to know a mapping between the record number and the desired record. Such a mapping is a study of a separate field called databases or file management, but we can understand the basics very clearly. I will request you once again to refer to C plus plus tutorials in the reference section. All the functions listed under CSTD IO in the handouts that will be released this week tomorrow onwards, you will provide a summary of all the functions and the file processing along with the listing of all the programs that we have discussed. Thank you.