 Hello friends, I am Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. In this video, I am going to explain how you can read structure from file and how you can write structure variable into a file. So before starting, if you go to description of this video, you will find links of pages, playlists, related to C language. So you can follow them. Now coming onto the topic. So I will be explaining two separate programs. So first program will be reading data from user and storing that into a file. So for example, I am going to read student data through structure from user and I am going to write that structure variable into a file. Then in second program, I will be reading that structure variable from file and I am going to write that structure variable into second one. So this will be the first process and this will be the second process. So let's start the first process. So before implementing main function, I need to write the structure because I am going to use this structure inside main function. So struct student, its members are rule number, integer type, main is of type character and then I am drawing it here. Then I am writing main function, inside main function, I am declaring file pointer so that I can open a particular file. Then I am declaring a structure, sorry, I am going to declare structure variable struct student S. So this struct student is a data type because we declared it before main and this is the variable of structure. So I hope if you are watching this video, it means you have already watched videos or you have not as related to structure. So I hope you understood what is the statement. So now first we need to open this file. Then we need to read the data from user and then we need to write that into file one. So now I am going to open the file. So name of file is file one dot pxt. I am going to open it in w mode because I am going to write data into it. And in setting program, I will be opening this into w mode. Now after this, you can check condition if fpw equals to null. So if your file is not opened due to any reason, whether you have not sufficient space or you have not, your file is not available in the system. So in any case, your file pointer is having null. Then you can print a message by null form and you can write exit so that the further steps will not be executed. Now after this, you can write a print test and you can display the message and enter roll number and name. And you can read that roll number to scan as %p and %s dot roll number. So this is the process through which we can read matters of structure. So we need to use structure variable then dot and then structure map. Then to read name, I am using letters as dot name. So this way structure variable s is containing a student name and its roll number as a rule. So now I need to write this structure variable as a whole into the file. So for that purpose, I am going to use a predefined function that is f write then address of s. So this is structure variable, its address we need to pass. Then we need to tell the size of struct student. So we need to define like which type of variable we are going to write into file. Then we need to tell the quantity. So right now this s is a variable. Let's say you have this area of structure. If you are writing this area of structure, so in that case, instead of writing one here, you will write 10. Because this area is containing 10 elements. But right now we don't have any area, we have just variable. So I am writing one and the file pointer in which we are going to write the data. So this s will be stored into this file pointer and this file pointer is pointing to this file one not used. So it means the data which is provided by the user will be stored into this file one effectively with the help of this f write. And you can see with a statement statement, we are able to write the data which is available into a structure. And after this, you can write as close fp. So then we close the main function. So as close with close, the open file which is stored, whose address is stored into fp pointer. So this way I hope you understood this process. So first we receive input from user and then we store the input data into file one. And now with the help of this file one, I am going to implement a steady program. So this time file one will be opened in read mode and file two will be opened in w mode. So whatever data we stored right now with this program into file one, we are going to read this and we are going to copy this into file one. So I am going to give you some statements. So this time we need two files. So I am using these. I need to find pointers because I am going to open two files. So this is f1. This is f2 then structure variable now f1 will be open into read mode. So then we can read the data which is available into it. Then f2 equals to f open. Here I am writing file 2.exe and it will be open in w mode. After opening both the files we need to flag if f1 w equals to null or f2 w equals to null. So if any file pointer whether it is f1 or it is f2 will be containing null. And this condition will be true. And you can write printf file not sum and exit one. So if exit is executed it means after this whatever you have written will not be executed. So if both the file pointers are not equal to null. It means they are pointing to a particular file. Similarly this condition will be false otherwise it will be true. So let's say if this condition is false. So control will be here and I am writing every so that I can read structure from this file. I am going to read structure into this s variable then size of structure. So I am indicating like how much memory a particular variable required and the quantity available is 1. So this amount of data one time will be received from f1 file. So f1 will be storing the data into this s variable. So whatever is available into f1 will be stored into s variable and this much size and single point. So if you have stored data of two students into pipeline. So the data of a student will be received off. So if you want to store multiple students data then you can convert this structure variable into add right. And here you can write f right address of s size of struct student 1, f2. So this time value of s will be stored into f2. And then you can write f close all. So this time we open two bytes with two file pointers. So if you want to close those files together so you can use f close all instead of f close. So this way I hope you understood how we transfer the data which is available into file 1 into file 2 with help of every reply. And these two functions are capable of reading structure from file and writing structure into file 2. So I hope you understood whatever I explained in this video. And you can implement both the programs in your laptop so that you can understand them well. And if you want to watch more programming related videos then go to description of this video. And there you will find links of various playlists. There you will find lots of videos related to C language so do watch them. And thank you for watching this video.