 Hello everyone, my name is Nikhil Gadjam, working as assistant professor in Department of Computer Science and Engineering in Valchanth Institute of Technology, Solapur. In today's session, we will see random access to file in C language. So learning outcome is student will be able to change a file pointer position using different functions in C. Now when we do any program in file using C language, it is important that position of file pointer plays a very important role. Now where file pointer is pointing from that point, reading and writing is possible. So in some cases, suppose you have opened that file, so file pointer will be at beginning or at end of the file. So at some point you need to change the position of file pointer for some reading and writing purpose. So there are three functions present in C. These are major three functions using which you can change a position of file pointer. First one is f seek, second one is f tell, third one is rewind. So we will see all these three functions one by one in detail now. First function is f seek, so this function is used to move the file pointer position to different positions either in forward or backward direction based on argument passed to this function. So syntax is f seek function takes three arguments, first one is file pointer, second one is displacement, third one is pointer position. So we will see what is file pointer, a file pointer whose position is to be changed that will be passed as a first argument to this function. Second it is a value either positive or negative. If this number is positive value means file pointer will move in forward direction. If value of this is negative means file pointer will move in negative direction. Now third one is pointer position. Now these are three different possible values for file pointer position. Whether it is 0 or 1 or 2, so you can pass these three values as an integer values or you can write seek set or seek current or seek end. 0 is equivalent to seek set, 1 is equivalent to seek current, 2 is equivalent to seek end. What is seek set? Seek set means from beginning of the file. Seek current means from current position, seek end means from end of the file. So example I have given is f seek in bracket fp, 10, seek set. Either you write this function or you can write f seek in bracket fp, 10, 0. 0 is equivalent to seek set here. Now seek set means from beginning of the file, fp is file pointer position whose position I want to change. Now 10 means it is displacement value, it can be either positive or negative, in this case it is positive. Now 10 means to move in forward direction from seek set, from beginning of the file. So if I execute this function, file pointer will move in forward direction by 10 points or 10 characters. Second one, f seek in bracket fp, minus 10, seek end. Seek end is equivalent to 2, minus 10 means negative value means in reverse direction. So if I execute this function, file pointer fp will move in reverse direction from seek end. Means from end of the file it will move 10 characters backward. Third one, f seek in bracket fp, 2, seek current. Seek current is equivalent to 1 value. The meaning of this function is it will change the position of file pointer by 2 points or by 2 characters in forward direction from current position of file pointer. Now guess the output of this program. Now this program will print valchan space Maharashtra, we will see how it prints. File star fp, so fp is a file pointer, it is pointing to file dot txt. I have opened file dot txt in w plus mode. When I open any file in w plus modes, it will open the file and file pointer will be pointing at beginning of the file. Now I have put as this function is used for writing a string into the file. So I have written valchan space solapur as a one string inside a file file dot txt. So after writing this string, file pointer will point at the end of file. Now I have executed a function f seek in bracket fp, 10, seek set. Now seek set means from beginning of the file plus 10 means move fp in forward direction by 10 characters. So after executing this function, file pointer will point to s. From beginning of the file, it will move 10 characters and it will point to s. After pointing to s, I have executed one function f put as function again, it will write one string. Now reading and writing is done where file pointer is pointing. Now currently file pointer is pointing to s. So from s, Maharashtra as one string will be written into the file. So after writing Maharashtra, now content of file dot txt will be valchan space Maharashtra. Now we will see the next function. Next function is f tell. Now it returns the current file pointer position as an integer value. Now syntax is f tell in bracket file pointer. Now example I have given, suppose hello word is a string and file pointer is currently pointing to d and I have executed f tell in bracket fp. It will return a long integer value. So I have printed that value. So in above case, printf will print 11 as an integer value. Integer value 11 is a position of file pointer which is pointing to d in this case. So f tell will return an integer value which tells current position of file pointer. Now see the program and tell me the output. Now question ask is what is the use of following program. I have executed this program, fp is pointing to file dot txt and I have opened file dot txt in read mode. So when you open any file in read mode by default, initially file pointer is pointing to beginning of the file. I have executed f seek in bracket fp, 0, seek end. Seek end means from the end of the file. Zero means displacement either positive or negative, zero means it is I have not changed the position of file pointer means by default now file pointer is pointing at the end of the file. Now length is an integer value. Length is equal to f tell fp. Now it will return a position of file pointer. So it will return number of characters present in the file because file pointer is pointing at the end of the file. So this program is used to find out how many characters are present in the file or we can say in other words a size of file can be tell by this program. So it will print number of characters present in the file or size of the file in bytes because one byte is for one character. Now last function is rewind fp. This function is used to move a file pointer to beginning of the file. So this is a direct function used to take file pointer directly to the beginning of the file. Syntax is rewind in bracket file pointer which is to be moved. Suppose hello word is a string and file pointer is by default now pointing to d. Suppose I have executed rewind in bracket fp as a function. So after executing this function now file pointer will move to beginning of the file means now it is pointing to h. Now these are different references I have used for creation of this video. Thank you.