 Good morning. In this session, we will discuss how files can be introduced. As I mentioned earlier today, files are often either not handled or are handled in a very rudimentary fashion. In fact, the students get to understand the files, primarily from the operating system in one point of view, that the files have names and the files are stored inside desks and they are an array of bytes and so on. And the only way they normally handle files is through redirection and that too only text files. The direct access files are rarely talked about in the first course. I suggest that this situation should be corrected while the due emphasis has to be given to handling of text files, but at least some introduction to handling direct access files must be provided in the first course. Before we discuss the files, structures need to be understood because we are going to deal with structured data, particularly in the context of handling information and information processing problems. We will then review the files very quickly. First, the simple files which we understood and then after discussing the sequential files, we will have a brief introduction to the direct access files, which are actually called binary files. This is a simple introduction that we often need to access different pieces of information related to the same entity and therefore, to access and process such pieces, we need to put these pieces together and treat them as a single entity. For example, roll number, name, let us say a batch, a laboratory batch or a tutorial batch and marks are attributes of a student and we would like to put them together and treat them as one single piece of information connected with the student. The notion of records in a text file, suppose I have created a text file which contains a roll number, name, lab batch and marks. I put them one after another separated by a blank space. Please note that it is always easy for us to say, I can read this line and I can read the roll number in one variable. The name in another array, the lab batch in still another variable and marks in still another variable. If I want to read all of these, I can have an array of roll numbers, an array of marks, an array of lab batches and an array of arrays which contain all the string names, all of these. If I put Mr. Anil in 0th element of an array, then this roll number will be kept in the 0th element of the corresponding array and so on. However, I am clearly dealing with four distinct entities and the fact that one particular element of all these four entities represent a single piece of information about one student is not very obvious to the reader of the program. It is here that we feel the need for the notion of a structure. We explained the notion of files, records and fields. These terms come from the data processing industry but these as I said are relevant increasingly for all branches of science and technology who do programming because they do handle such records, fields and files. Let us consider this text file which I just showed. It contains students data. Each line contains a record for one student and each record has values of several attributes, roll number, name, batch and marks. These are called fields of a record. In fact, roll number, name, batch, marks, these names are actually treated as metadata or data about data. They represent columns in a table where the rows in a table represent the values pertaining to a particular student. So, in short then several fields make a record and several records make a file. Each record in this file has information of the same attributes roll number, name, batch and mark. So, different records in the file will not have different pieces of information or the information arranged in any haphazard order. In any record, the first field will be roll number, next will be name, next will be batch, next will be marks. The name string can have different length for different students depending upon the actual name that you get. So, here is a pictorial representation again. This column is roll number, this column is name, this column is batch, this column is marks, but this row is the record number zero representing the record of student Anil. This next row or line number one is the record of the student called Amit and so on. There is no method that we know so far because we have not introduced our students the notion of struct. There is no method by which we can consider a record as a single entity. It is our endeavor to ensure that we use the C C plus plus programming facilities to represent a record per se and are able to describe the components of this record which are roll number, name, batch and marks. Notice that in a text file, all the information is stored in the form of plain text, but internally I would most value treat roll number as an integer number, batch as another integer number, marks as a floating point number and name as a character array. So, I have to make a motley cloud of these four disparate things and somehow ensure that all the four elements of information about a student are made part of a single record. This single record kind of concept is nothing but a structure or struct in C. Struct is useful in reprinting entities which may have many attributes of distinct types. So, as I said we wish to represent and handle some basic information about a student like roll, name, hostel and room. We can define an abstract data type comprising this field. The way we define it is we say for example, student info is the name we give to this abstract data type. We say struct meaning student info is now being defined as a new type which is of the type of a structure as signified by the word struct and inside the brasses we write various components of this structure. For example, the first component is a roll number. So, we say int roll. The next component is a character string reprinting name. So, we say care name 30. The third component is batch which is an integer value. The fourth component is a floating point variable called marks. Notice that ordinarily these are all disparate different things, but when put in this fashion these four elements constitute a struct type called student info. Once we define this, remember this is a abstract data type. So, like int or float or care, I can actually define variables of the type struct student info. So, variables can now be defined of this type and then they can be used. So, let us see how do we do that. We say struct student info s. This is very similar to our saying float x or int y. When we say float x, it means x is a variable which is of the type float. When we say struct student info s, it means s is a variable of the type struct student info. That means s immediately represents an entity, a variable which has four components. The roll number, the name, the batch number and marks. How do we deal with individual components of s? Because it is the components which will have distinct values. We will have to assign values to these components. We may want to print those values or output those values. We may want to manipulate those values. We need to be able to therefore, access and manipulate the components. This is possible because s is now called the structured variable. Its components are denoted by putting a dot between the name of that variable and the name of the component. So, s dot roll becomes the roll number of this variable structure s. Its value will be integer because the first component was defined as integer. s dot name is an array which will hold the name of this. s dot batch is again an integer value. s dot marks is a floating point value. It is therefore, this s which is a variable of the struct type which actually is building a motley cloud of these four people as a single entity. And just like any other type, the struct student info also has a size. Now, what is the size? The size is equal to the total number of bytes allocated to a variable of this type which will be essentially the sum total ordinarily of the bytes required for individual components. How do we find this size? There is a function available in C plus plus which says size of struct student info. So, you actually name the type inside just like you can say size of int, size of float. Notice that int might have different representations on different machines. Somewhere it might be 2 bytes, somewhere it might be 4 bytes. So, when you say size of int it actually gives you an output which is a number which says so many bytes constitute an integer type. Similarly, when you say size of struct info, it will actually give you the size in number of bytes. In this case, the size is 44 bytes. I have raised the question why? To understand why I am raising this question, let us go back briefly and see what that structure is. It has int role. We understand that integer is ordinarily 4 bytes. It has care name 30. This is 30 bytes. Int batch is 4 bytes and float mass is 4 bytes. Unfortunately, when we add up these component lengths, this is 4 bytes plus 4 bytes plus 4 bytes, 12 bytes and this character array 30 is 30 bytes. The total should be 42. However, if I say size of, I will get 44. The reason is that in most of the computers, while elements of a structure will be given consecutive memory allocation, the allocation to individual components is made as per certain rules. The simple rule is that on most machines, the beginning address of a location for a particular type of variable is often aligned on the what is known as a word boundary. That means, a variable location address will begin at 4 byte distance from the previous rule. Now, if you go back to this particular thing, let us consider this here. The role is 4 bytes. Care name is 30 bytes, but 30 plus 4 is 34. It is not divisible by 4. So, 35 cannot be the starting address of this batch. The batch must start at an address which is divisible by 4. So, there are 2 bytes which are lost in between. Nothing is kept in those 2 bytes. Those 2 bytes are sort of alignment bytes and therefore, the batch starts 2 bytes. Later, mark starts of course 4 bytes because this is already aligned on the boundary. Consequently, the total size is not 44 bytes, 42 bytes, 44 bytes. Here is a program to calculate the rec size. As I said, I will define this structure, define a variable called S for this structure. I define an integer variable called rec size and I execute this statement. Size of any data type will give me the size of that data type. So, structure, student info being a data type, this will give me the size of that data. Before going further, I am going to write a program which actually reads the data for students and prints it out. It reads the data from a file. We will introduce file shortly. However, we propose that we read these data in different elements of a structure and then when we want, we want to print the value of a particular variable which is a structure variable. So, consider this function which says print student. The parameter that is passed to this function is not individually roll number, name, batch and mark, but a structure variable is passed. So, just like we say int x as a parameter here, we will say student info S. So, the variable passed is S which is actually a structure and inside if we use printf or cout statement, we will not use roll name etcetera. We will say s dot roll, s dot name, s dot batch and s dot mark. This will print the four components of the a particular student whose information is passed through this variable S which is of the structure student info type. It is at this stage that we introduce the notion of a file. First of all, whatever is said on this slide is something which is already well known to the students. Namely, a file has a name, it has an extension such as dot t x t dot e x c dot t whatever, whatever. It has a path, it has permissions for reading and writing. The file has a size which is equal to the total number of bytes. This is the information which generally students would already know. The physical location of a file and its properties are known to the operating system because it is the operating system which is the custodian of all files. Now, as far as C plus plus is concerned, it treats a file as a large array of bytes. It is very important, interesting and important to note that as far as C plus plus is concerned, whatever type of file you talk about, for it a file is an array of bytes. That array has a name which is a file name. File name is a peculiar entity which is a pointer. I will explain that, but C plus plus treats the file as a large array of bytes. Now, these bytes may contain character data in which case it is a text file or they may contain an encoded data in which case they are called binary file. Consider for example, if we wish to push the internally stored information about a struct S to a file. Now, struct S does have text component. It has name which is text. However, the roll number is an end, the marks are float and all these information represent internally as per a floating point format or whatever constitute a series of bytes. Those many bytes is the size of a record which will occupy when I push a particular value of S or to a file. We can also use the facility of files to store special digitized data such as voice of images. We already know that image data can be stored in this fashion. The files are handled in a very peculiar fashion in a program. Unlike other variables and arrays which have a name that we give, files are always handled through a pointer. So, whenever you want to use a file, you declare not a file name, but a name which points to a file. The keyword F I L E written in capital is almost like any other type like interfloat, but it is a pointer type. So, just like we say int star, you say file star p, file star in file, file star out file. We use appropriate names to indicate a file from which we will read data in. So, it is called in file, a file on which we will dive data out, so called out file. Of course, any arbitrary name can be used by us. Now, a file can be opened, records can then be read or written to that file and at the end it should be closed. In short, conceptually a file is like a cupboard. There are multiple cupboards available. You name a cupboard by a name. In your program, when you say open that file, it means open that cupboard. Once a cupboard is opened, you can read data from various components of that cupboard. You can write data to various components of that cupboard and what are the components? These are merely a sequence of locations which is an array of bytes. The read write operations are done through special functions. There is an internal index which says at which location we are at the moment where read or write will take place. These internal indexes automatically incremented after each operation in case where the file is open for sequential read or write. All text files by definition are open for sequential access only. That means although I compared a file with a cupboard, when a cupboard contains a text file and the cupboard has many locations, I cannot arbitrarily go to any location there. I have to sequentially go through all the locations one by one, read or write them and then close the file. However, a file which is not a sequential type can permit me to go to any particular portion of the cupboard. I will explain that through a brief small diagram. This slide describes what is meant by the direct access of records to a file. As I said, a file is a series of bytes. It is an array of bytes relatively independent of where the file is located on the desk as far as our program is concerned. Whenever we open a file, we presumably are at this point which is the 0th byte of the file. This is the first byte, this is the second byte, this is the third byte and so on. Now how do we know in case of a sequential access or for that matter at any point in time, where are we pointing to in the file? This is done by a position value. So we can go to a position and access any number of bytes from that position. That is the essence of direct access. Imagine a thing called POS, a variable called POS which is a numerical value. The POS value points may be to the current position where we are. We can change the value of POS and use it appropriately in the dysfunctions and this pointer could point to any byte. Do not confuse this index with the name of the file. The name of the file represents this entire entity. There are special functions available in C plus plus which permit us to go anywhere. For example, I can use a function called fseq which sets the position indicated by POS for accessing the file. Please remember that POS is actually invisible to the actual functions which read or write data. I have to set the position appropriately through fseq function before doing any read or write for directly access file. The POS represents the displacement value which is specified relative to either file beginning which is represented by something called seek set. This is actually a predefined value. These values are 0, 1 or 2 for seek set, seek curve and seek end. I will explain very briefly how exactly these are used through an example. The functions f read and f write read and write specified byte at that position. So, the position has to be set independently using fseq. Once the position is set, whenever I say f read, the bytes will be read from that position and whenever I say f write, the bytes will be written at that position. Whether reading or reading so many bytes as prescribed, the internal position is automatically advanced by the number of bytes which I have read or which I have written. These are the functions for handling files which we call the direct access files or sequential access files. For example, f open, this command opens a file and it requires the file pointer as a parameter. So, if I declare file star fp, then a statement of the type fp is equal to f open followed by parameters which are prescribed as part of f open. These parameters are the name of the file which is known to the operating system and the mode in which the file is open or for example, means I want to read from data from that file. How do we know that this is executed properly? It depends upon whether after executing this statement fp gets allocated a value which is a proper pointer value. If for some reason this opening business fails and operating system throws up the hand saying I cannot open this file, fp is set to null. It is invariably a good practice after opening a file to examine whether the file was indeed opened or not by just checking the value of fp. This is shown here in case of another file which is described here file star fp out. The name suggests that this file is going to be an output file. That means I am going to write something on it. Observe the way the file is opened fp out equal to f open dv dot bin rb. Now, this is the name of the file dot bin extension means it is a binary file rb means it is for reading it is open for reading. So, somebody has already written the data there b means it is a binary file it means it can be directly accessed. Here again the fp out the pointer may or may not have proper value depending upon whether open succeeded or not. And I can say if fp out equal equals null then I say cannot open output file. Notice the dichotomy here I think many of you would have noticed that these lines are not written correctly. Even though you may not know the exact syntax of fp out in fp you should be able to spot the error. Those of you are not able to immediately do that please go in the evening in your lab and in the lab you have a lab handout wherein many programs containing these statements are there and then cross check what is the correct syntax for you. Here is a typical reading process for a file. Read record number k from a binary file. At this stage let me revert back to the notepad. So, this is the disk my files will ordinarily reside somewhere here. So, let us say this is a file called student dot bin. When I say open the file the file will be open and a file pointer will be associated with it. So, I will use the f open statement and I will assign it to some file pointer which I have defined let us say fp. When I open the file the file will open to me something like this is that array and there are bytes here and so on. Notice that I although I can read and write single byte that is not the objective I would like to read or write a group of bytes. I would like to read or write a record. For example, let us say these many bytes constitute a record. Then I would like to read the group of bytes like this as many records as I have. Let us say this is record 0, this is record 1, this is record 2 and so on. Each record has a fixed size. Now you can relate this to the notion of struct because the struct size is actually the size of a record which I would like to read or write from a file. Ordinarily as I told you about the position when I open the file my position is actually located here. As I read or write this position changes as I said it is a hidden position. But I can use the command f seek to set the position anywhere here I want. I can set it arbitrarily to 1000 bytes it will go here to 1000 bytes, 1 millionth byte it will go somewhere here. Of course I would like to position it in the context of the records that I have. So what I will do supposing this is record which has the length 44 bytes and I want to position it at this record. So 44 plus 44 is 88 and I should then position this 0th position is this first byte of this record. So 88 will be the position here. I can calculate that using this struct. How do I set the value of position which as I said is posth? First and foremost posth is defined as a large end. So it actually can represent a very large value. It is in this context. So what I will ordinarily do is I will first set posth to something. Then I will f seek to that position. Just by giving a value to the variable posth does not mean that my internal position of the file has changed anywhere. The position indicator remains wherever it is. Posth is merely my notion of a name of a variable where I want to put a value. After I put the value it is f seek function which will move this position to that position and after I do this I can do either f read or f write. In the context of this explanation just see this small sample few lines written here. To read record number k from a binary file I define long int posth. Then I open the file. I presume that I have defined the file pointer to be fp. I say f open fp filename.bin,rb. As I told you after opening the file I should check whether the file indeed opened or not. Now suppose I want to read the kth record of the file. I am calculating posth as equal to k minus 1 into the record size. So k minus 1 are the previous records. Since first position is 0, second is 0 plus record size, third is 0 plus 2 record size and so on. So k minus 1 into record size will point to the kth record. What is this record underscore size? You will recall that rec size is what I calculated from my structure. All that I have not positioned the file. I have merely calculated the value of an internal variable which is known only to me. The file system has nothing to do with this variable posth. Only in my mind I think that this denotes the position of the internal pointer to the file. After calculating this I use the word f seek. f seek fp, posth, seek underscore set means seek underscore set means starting point of the file. From the starting point calculate so many bytes and set the internal indicator or internal pointer or internal index to that position. Now the file is positioned for me to read or write a record at that place. Since this file is opened as a read file I cannot write anything but I say f read. Now observe the parameters of f read and s. This is a pointer to the structure variable whose record value I want to read. Rec underscore size, this gives me the size of the record or so many bytes I want to read. One, it gives me the number of units of this record I need to read. What does it mean? I can read 10 records at a time if I want. The total number of bytes read will be the number I write here multiplied by the rake size. Of course in that case I should have an array of struct as this parameter. I will still pass the parameter as pointer to the zeroth element of the array. In this particular case I will read only one record and the file pointer from which I have to read the file is actually the last parameter of this function. So f read and s rake size comma 1 comma f p will read from this file from wherever the position is currently at the file which I have set by the f 6 statement. It will read so many bytes and it will pass on those many bytes to the structure variable s. Notice that this way I can read or write arbitrarily at any position in the file. This is called the direct access file. There are two examples which I have given in the handout. These are the two problems which that those examples solve. The first problem says read records from a text file, extract the four component values from each record which are these four component values the same which we described earlier. Role number name batch number and marks of a student and there are multiple students listed in that file. In fact the same data is given as a sample input five in the handout. So read those records from a text file, assign these to appropriate parts of a structure and create a binary file called student db dot bin. db generally stands for database. In fact those of our students who will study databases later will understand much more about these direct access files. Dot bin is not obligatory. I need not give an extension. It is like any arbitrary name that I choose. It is customary to write dot bin as an extension for binary file. But as I said it is absolutely not essential. So this is the first problem. Read the data. The data reading from that text file is done as if I am reading it sequentially. Every time I read a record I should create a stuck record and push it into the binary file. This is the first problem. Having created this now I let us say want to search for information about a student. I give a role number and I want to write a program given that role number. It will search and extract information only about that student. One I can read the file sequentially record after record. I want to find the marks of a student given the role number. So the first part is sequential search or desktop. The program that has been given as a sample program actually contains instructions to do that. Second, read a student record at specified position by direct access and update or modify the marks and rewrite the modified record and verify. Now this is interesting. There are records and by the way in actual practice for example, consider all students who take the joint entrance exam. There are 4 lakh students. So there are 4 lakh records in the day. Now consider you want to find out the marks of a particular student. You have a choice of sequentially reading all the records of a file. On the other hand if you know what is the record number for that student you can directly position the internal pointer to that point and read a record. Minimal time spent. The disk is capable of giving you directly that interming. These direct access makes the access extremely fast. So the second point says read a student record at the specified position by direct access. Not only you read the record, but after reading the record you want to update the mark. Let us say somebody said look you have given me 91 marks, but actually I got 93.5 whatever. So you say all right I have verified everything. Now how do I change that? Remember the data is already written in the disk. I can then open a 5 in what is known as a read write mode. That mode permits you to update information in place. So you read a record and you rewrite the record in the same position. There are some interesting speakers for example, when I read a record the read the very read operation will actually shift the internal pointer by as many bytes as the record that I have read. So before I rewrite I will have to bring back that position by another F C to this place and then rewrite. There is an additional problem. I can access individual records by giving the record number, but I have to access the records because I am given only the roll number. How do I know which roll number is in which record? That means I will require a mapping from roll number to record number. This is an entirely independent field in itself. In fact the notion of index file and the notion of creating indexes is related precisely to this kind of correlation or mapping. Given a roll number find its record number and we do not want to do that by reading all the records and saying this roll number is found. So the record number is 154. That means the purpose because I want to directly access 154 record. So the mechanism of this mapping has to be outside the file itself which contains all the data. Anyway this is the max data dot txt file. This file has been included in your handout. So this file will be used automatically as an input file by the first program which will create the binary file. Here is a sample program in a more visible fashion. The handout is small page. So you may not be able to read everything. Reading records from a text file and writing to a binary file. Notice this. I have not given the declarations and so on. It starts with in count equal to 0. It does an F scan F from f p underscore input. This is clearly the file pointer associated with the input file. An input file is a text file none other than this file which contains this text. So I would have opened I have to open it by giving the name max data dot txt and then read data from it. So this is what it attempts to do. It says F scan F. Scan F is the ordinary input function from keyboard. F scan F says read from a file and the first parameter of a scan F is the file name or the file pointer. So I say F scan F F p underscore input and the remaining statement is exactly the same statement that I would have used to get these various components. Remember this is a text file. So I read this in terms of the parameter that I give because the number of blanks that I will put are the number of characters in a name will differ. I do not know this is not a fixed length record yet. It is a text file already. After having read these components I want to construct a record. So notice how I am doing it. While the file has not ended I take the input that means the I have actually got a record therefore the input is valid. I increment the count and now I do the interesting thing. I assign the value of r which I have read from here and r to s dot rho b to s dot batch m to s dot max and I copy the array that I have collected the name in n is obviously an array into s dot name. So all the four components of my structure have been filled with values which I have just read for one student. Having read this I output that record I say count that is the first record second record whatever and I say print student in bracket s. What is print student? You remember we have written a function print student. So it invokes that function takes s as the parameter which is the structure to which values have been assigned and this function will print the separate components of s. Having done this now I create a record I write the record in the binary file see how it is written f write and s comma rake size comma 1 f p underscore output. That means in the file pointed to by f p output whatever is the current position you write one record of the size rake size and from where from a structure which is pointed to by the address address. So very simple I have constructed a record inside my memory and I am writing it on to the file. Please note that as I said every read and write operation automatically advances the internal position pointer or position indicator. So when I read the next record from the text 5 and go back to execute this iteration again the second record will be extracted it will be written and so on at the end all records which were there in the text 5 will now be written inside. The difference is what I read where text value as text input but what I am writing is a composite structure role is integer binary number, batch is an integer binary number, marks is an integer floating point value and name is a string array. But all these composite form a record of the size rake size which is written in exactly a similar fashion. I can read these records sequentially I have already suggested how direct access can be made. Let us very briefly look at the file handout. The first page of this handout tells you about the standard library functions which are stored in CSTDIO. Those of you who are familiar in this standard C programming would remember saying hash include STDIO dot H. The header files which were to be included through compiler directives always had dot H suffix in the traditional way. However in C plus plus if you want to use those header files which belong to C type functions then the names are different the library name is simply CSTDIO. This contains a very rich set of library functions which permit you to do variety of things. As I mentioned here I just want you to show the names of the functions so that you can read them f open for example this opens a file. What I have done is I have consolidated the name of a function and example of using that function in a program and then describe what those parameters mean. For example if the mode is r it means read, w means write, a means append, text file is the default. If you want a binary file you have to say b all that is written here. It returns a pointer of type file and it returns null if the file cannot be opened. I am sorry I cannot show you the entire document but please at leisure read this thing it is on the model. So even after you go back you can access the model download this handout and read it. Of course I urge you to read from standard text books. Sadly Professor Abhiram Ranade in his book on C++ does not spend too much time on files but there are several other books and most importantly the C++ open tutorial on the internet. It is an open access tutorial and you will get all the information that you need. What I have tried to do is to put concise information here along with some examples you should be able to download this print it and read it at leisure. Here my attempt is only to show what kind of functions you have say of f open, f close. Remember whenever you open a cupboard at the end of your job you must close it properly exactly the same way with files. Of course C++ is very decent if you forget to close a file and get out of your program at the end of the program normally C++ closes your file. Here is the f seek function which I have just mentioned. So it says f seek fp, pause, origin. Origin means with respect to what point you are describing the pause that is where the seek curve, seek set, etc occur you can read more details about it. F tell is a very interesting function. You are in the midst of the program and you have forgotten at which particular point in the file you currently are. Remember the file knows where the current position is but that is invisible to us and we have forgotten how we had calculated pause and used f seek earlier and so on. All that I can say I use this function f tell f tell fp will return the current position. So I know what is the current position which I can assign to pause. So these are interesting functions that are given. A rewind will bring the position back to zero. f read and f write are the two functions which read and write record the binary file. The normal standard function that you have get s or get line or get c, etc, etc are available in this library so that you can handle the text which comes from a file instead of being typed on the keyboard. f scanf and f printf are the file related functions of scanf and printf. As I said scanf will work on keyboard input printf will produce an output on your screen but f scanf and f printf will read and write respectively on text file. Here is a program which creates the binary file. By the way this is a PDF file but my colleague nagesh karmali has also inserted these individual files as not cpp text file. So when you download and untar or unzip the bundle you will get not only the PDF file with this handout but you will also get two cpp programs one which creates the file and another which actually reads a specified roll numbers record and so on. You also have a data file which is the text file so you do not have to type in either the program or the data just explore it run it and have fun you will see how binary files work. I urge you to do that today because this is not ordinarily what you would be doing very rarely I have seen students and teachers dealing with direct text file. Surely it is less important than variety of other important concepts which must be well understood by students but it is so important in later life and particularly for students of non-computer science and non-CS who will hardly be handling files in their programming it is useful to give them some indication on how to use it. I think I will now use the remaining ten minutes for some interaction this is MPST ME at Dhule. Hello good morning sir sir my first question is what data types we have to use if a value exceed 18 digits? You have no data type you have to write special package which we say is a high precision arithmetic package in fact that is a major limitation and whatever number suppose there is a compiler which permits whatever 80 digit kind of value then I will ask you what do I do if I have 85 digits what do I do if I have 120 digits the limitations are very well known well established in fact that is a major part of study of all your numerical analysis work because the limited precision in the CS 101 x course which I will conduct and which as I suggested you and your students are welcome to register for it as audit students I have a session in which I describe how high precision numbers large precision numbers can be represented and how simple arithmetic can be performed on such high precision numbers but within the c c plus plus there is no facility for representing this this is Silguri Institute of Technology. Hello sir good afternoon sir I have I am having the question that last session you have discussed that we are having one structure and it is having the content int role care name and at a size is a 30 int batch float mass and you are telling that that if we write the c out size of student info then it will give the result 44 byte but I think if we give the c out size of student info it will not give the 44 byte it will give the original size 42 but originally when we store some record in this structure that time it will take the 44 byte. Just now I have executed this program also and I have seen that in my system I have executed that one in tc win 4.5 and there it was giving the result 38 byte why 38 byte because in this case it is taking the role and batch 2 2 byte so 2 plus 2 4 byte and for name it is taking the 30 byte so 30 plus 4 34 and mass it is taking the 4 byte total 38 byte and it is showing the answer also 38 byte. I got your question you forgot one thing you forgot to tell us your name. So very nice to speak to someone so far away from us physically thank you for a good question let me answer this what our friend is saying is very interesting suppose I have something called int a float b int c now when I discuss these things in the context of my system sample program I had assumed that int a require 4 byte int c requires 4 bytes float b requires 4 bytes in her system int a requires 2 bytes int c requires 2 bytes float is probably 4 bytes so this is system 1 and this is system 2. I can assure you my dear lady from Siliguri that you will it is not impossible to find a very high end super computer which the c plus plus compiler implementation is such that integer is 8 bytes floating point is 64 bytes and this integer is 8 bytes this would be system 3 and that is the whole point these lengths these allocations are compiler dependent and machine dependent they are not uniform and that is the reason why what you use on your machine is best reflected by writing the command writing the function whatever you want to know you should find out from your system only your system knows what is the size of int what is the size of float what is the size of int and I thank you for asking this question because it gave me a chance to emphasize that different computers and different compilers working on those computers will allocate different sizes for the same type of data but whatever type of data and whatever is the allocation that is done on your machine that is what the implementation of size of will return to you. So, if you say size of struck whatever and calculate it into let us say size now this variable value in my case it was coming 44 in your case it is coming 36. When you claim that it will come 42 and not 44 you are basing your observation on the assumption that I do not require to align things on a 4 byte boundary please understand that on your machine the boundary is 2 byte try to allocate an odd number of elements of the array and put that array of characters in between 2 integer values you will suddenly find that it will not measure up exactly equal to 2 plus 2 plus let us say 29 bytes or something like that there is an alignment which is usually forced on numerical type however the point is that these allocations including the allocations within the for the components of a structure are entirely dependent on compiler and machine. So, we cannot generalize let us take a couple of more questions thank you for this good question. My name is Kirtika from PMC Tech OZO as we know that the C and C plus plus both can run and GCC compiler and also the turbo C compiler but it varies in header files and also some of the codes regarding the pointers it gives various results in GCC compiler and turbo C compiler actually our we have for the second year students itself the C and C plus plus how the students can get the impact of these difference please can you explain again a good question but very much similar the answer is very much similar to what our friend from Siliguri asked the pointers point to internal addresses and therefore these internal addresses depend upon how the memory allocation is done to different variables of the different type this itself is compiler dependent I mentioned that it may be machine dependent that is one factor but it is also compiler dependent on the same machine you use GCC compiler you will get one set of values on the same machine if you use turbo compiler you are likely to get different kinds of unfortunately C plus plus language officially permits this it says these sizes and therefore the memory allocated pointers etcetera etcetera are implementation dependent that means either the machine or the compiler if they are different then you will get different results but I will tell you what you should tell your students is they should not worry about this as long as the calculations are concerned as long as the control flow is concerned C C plus plus will behave exactly in a similar fashion the problem will come if you create binary files at one place and try to read them at other place and if the binary file sizes are not same then you will have problem it is for this reason that often the components also are defined as text components and you actually write ASCII code for the numerical value just like you put in the text the only thing is you do not permit variable size string however you create a binary file you read and write a binary file but you ensure that the record size of that file is fixed across all machines and compilers and that way for that you have to force the size you cannot depend upon the size determined by various components but as far as individual machines are concerned if you are creating local struct variables or local struct arrays which exist only for the duration of your program execution there is no problem it will not affect the result but yes these things are compiler dependent and implementation dependent. My name is Amit Hirawat sir sir I am I wanted to know about dangling pointers and the precautions we should take to avoid dangling pointers. Dangling pointers generally refer to a pointer which does not point to anything this can happen generally if you assign to a pointer an arbitrary absolute value yourself without getting the pointer through the address operation. This can also happen if you have defined a pointer variable and if you are using some advanced features of the programming language such as memory allocation now when a new memory is allocated to a structure or a variable it is brought into existence by the compiler at run time and the memory is actually allocated at that point. Now imagine if you have done that and you have assigned the pointer to that through some address operation to some pointer p now later on in your program you destroy that object or you reallocate the memory now that object is gone that memory is reallocated by the run time system to something else but the pointer variable in your program in which you had extracted that old memory address is still holding that value now that suddenly that pointer becomes pointing to nowhere or in fact it might be pointing to another new object which your program has created later which is all wrong. What is important how to handle these dangling pointers is to ensure that whenever you reallocate memory which your program has allocated all the corresponding pointer variables or various which you have used in your program and to which values have been assigned pertaining to these objects should not be used unless they are reassigned value. In short in your code segment you should always ensure that a pointer variable is used very close physically to a place where the pointer variable is assigned a value you cannot assign a value to a pointer variable thousand lines before and use it sometime later you should be able to aware where it is so that is one precaution. Second is a pointer which has a null value you should be extremely careful about null pointers are the pointers which cause maximum havoc in programming. Sir if we write a statement like int star int star s equal to some string int pointer s equal to some string in double quotes now that s is the s initially contains a garbage value so is that that the string is stored at a garbage location? No I would like to ask you more fundamental question why would you do such a silly thing at all in the first place? No but if you want to store a string into a pointer character pointer s is equal to a string character pointer s equal to string ok. Now the string is stored the first character of the string is stored in the location s the question is is s a garbage value? Ok I get what you are trying to say I think there are many other colleagues who are also wondering what you are trying to say I would suggest the following I would say that the pointer variables should always be used in the context of other variables and arrays and not in the context of constants. Constants are known as constants let them be known as constants I do not see any reason to necessarily get a pointer to a constant I do not see any programmatic use of it however it is interesting to do a small experiment on the machine and check what happens I have already indicated in the sample program that you can print pointer values and you can see whether they make any sense or not and you can get also what pointer points to but the fundamental programming decency is in ensuring that when and if I use pointers they are obtained values for only through the address operation on some variables and array not by direct assignment KIT college of engineering sir. So my question is regarding the pointers sir what is the difference between care star and unsigned care star. I tell you I I am not absolutely very sure because in my mind both should point to the same thing if you talk about different types say I will give you an example int and long end or float end level these have different number of bytes allocated to them and therefore that pointer when you define see the purpose of defining a pointer of a particular type is that when you do a pointer arithmetic operation add one or subtract one it should actually move forward or backward by so many bytes. The only difference between care star and unsigned care star I do not know it still occurs to me that both care and unsigned care should occupy the same number of bytes in terms of numerical values but internally if the type is distinguished then you can use the care star pointer only in the context of care variables and you can use unsigned care star pointer only in the case of unsigned care variable. In general I have seen unsigned care being used to represent pixel values in images but whatever it is that type is associated with a pointer permanently and whenever you are using that pointer you must use it in the context of that variable only. I will also try to find out more about this. So myself Sameer Patil in Linux environment as the pointers deal with the logical or the virtual addresses how could be dangerous to deal with the pointers it many times been suggested that this pointers may raise the security concerns. In Linux environment they are dealing with the virtual addresses why they could raise the security reason. Thank you. A pointer inside our program does not deal with a virtual address. A pointer inside our program relates always to a concrete object which has been created in the memory. It becomes virtual only because you might make a mistake of the kind that I mentioned that you have you have allocated memory to something then extracted its address and allocated to a pointer and then you have deallocated that memory that is when the pointer becomes virtual but otherwise if you are careful there is absolutely no problem. The notion of a security risk is not created by the fact that there are pointers in the programming language. It is created by the wrong use of pointers. So do not blame a programming language feature for the security risk. It is our inability to control our program. My friends incidentally just mentioned that in so far as storage is concerned both unsigned care and unsigned care star and care star will still have 4 bytes of memory except that the distinction is only that you can use one pointer only in the context of that particular type. Frankly we do not know if there is any more difference but we will find out. We will take two more questions again. We are going to Selvam College of Technology. Good afternoon sir. I am Raj Narayan sir. My question is regarding pointers. So I want to know how should the null be defined on the machine used as a non-zero big pattern representing the null pointer. This is now my question number one sir. I would be highly surprised if a null pointer in any implementation uses a non-zero representation I have not come across that. By definition null is an absolute zero. Any other value will symbolize some specific value. I have not come across it. Can you tell me where you came across a non-zero null pointer. In fact the word NULL capital means zero. So whenever you compare a pointer with null it is actually being compared with an absolute zero value. Is there some place where you come across a non-zero value over to you? This is a question actually arised by my student. When he attended some interview he asked this question to me. So just I want to clarify that which null. Good, good. So the right answer to the best of my knowledge is that there is no such thing as a non-zero null pointer. A null is always a null and if a pointer contains any non-zero value it is not null. It may not be meaningful. It may be a dangling pointer as our friend said. But a null pointer is a null pointer always. We will go to, thank you so much. We will go to one more institution. My name is Suma from St. Joseph's College of Engineering. I have two doubts. One is regarding the springs which I plan to ask in the previous section. That is in turbo C sometimes when we use get us command, get us function, the function will not work. Usually the statement will be skipped and it will take the next line onwards. Why this happens? I have no idea how that function works anymore. My own example programs were created about three years ago. As of now the behavior of get us is becoming increasingly funny and interesting. My recommendation is to avoid using that whenever you want to read a string use get line which is now standard and recognized by everyone. But I will try to explore it. Unfortunately we do not have a turbo environment but it will be interesting to do that experiment and find out. A friend of mine has an answer. Being an M. Tech student I think he must have recently dealt with turbo. What he is telling us is that get us actually reads from the buffer. When you start the program it assumes that there is an end of line and that is what it might call to skip a line particular. So his suggestion is you just say flush before saying get us. The flush function will flush the buffer and the next get us will work properly. I think that is interesting and useful. So basically the problem is happening that the get us function in turbo environment is not necessarily always reading from S.T.D. in all terminal or five but it is reading from the buffer. So if you have some previously rate thing it might take the end of line from the previous buffer and skip the line. So I think his answer is correct that if you use flush command the flush actually flushes the buffer out. So that the get us will necessarily force yourself to go to the actual file. I suspect that even get line might have the same problem. If turbo has an habit of reading from buffer it will implement everything the same way. I do not know. No. So he is also more confident about get line. The advice is use get line. Thank you. Anurag Group of Institution. Good afternoon sir. This is Balram from Anurag Group of Institution. Regarding RA's previous session in the morning session I have it out. We declared an RA of size 10 when I take the input into the RA. Maximum increase up to the size. So when I take the input above the 10 size of the RA it will taking as the input. So how we can control this one? You cannot control that. There are mechanisms by which you can define the maximum number of bytes to be read etc. etc. in case of file processing but otherwise the care has to be taken only by the input operation. The standard trick is to define a much larger array as a temporary buffer internally inside your program. Read input into that array and then examine the contents yourself and transfer whatever string you have read to some other array. But there is no other clean way. Automatically you cannot control that. The C++ implementation expects you to take care. All right we will just take one more question. There is one more question from the same center. All right please go ahead. Sir when I am allocating the memory allocation by using malloc. So when I am releasing the memory so simply we are passing only starting address of the location. So how the memory are free can recognize this is the end of the location. For example I have allocated 20 bytes of memory. So simply we are passing starting address but how it recognizes it is a 20 bytes up to that. You are not passing a starting address. You are passing a pointer and the pointer is associated with that type. The pointer remembers how many bytes were allocated. If you are passing an absolute address what you say might happen. That is precisely the reason why pointers are used. So you do not have to worry about it. I am glad that you remember to deallocate memory. I have seen people who keep malloc'ing and never release it and get into lots of problems. But the answer is this that you are not actually passing an address. You are passing a pointer and the pointer being associated with what you originally allocated. It knows exactly what is the size of bytes that needs to be deallocated.