Hello and welcome back to this practical module of C programming course…… And we have come here with a new practical in front of you, where we are going to discuss a practical related to File Handling. Where you'll get a chance to see the operations of read and write. Along with that, you can even be able to implement it in your text editor and you will be able to write your own C program. So, without any delay, we will start and we will see what will be our practical’s names, its name is read and write data in a file. So, what are we going to do here is we are going to create a file and we are going to open it and we will be input some data in it and then we will close it. So, after putting the data in, the file will remain there as it is. So, we will open that file again. And this time, we will retrieve the data from there, which means we will read it and we will get it in our program. We will see that whether we can do it successfully or not. So, to see this, what we have to do is we'll have to go into our text editor. So, we will quickly go into our text editor. Now, we have come into our text editor. And you can see that we have made a basic template and we have kept it ready in which we have used two preprocessor commands, the first command is stdio.h file and along with that we have used conio.file, which we are going to use related to the functions of file handling. So, this was about a preprocessor command. Now, we will come on in our main function, here we can see that how we make a file, but first of all we will have to see that, is their file name already there in our computer already have the file of that name or not. So, on whichever location this programme is currently there, so, you can see that here there is no file with us whose extension would be .txt. So, definitely here there is no file for now. But as and when we go ahead in the program, you will be able to see that how we write those lines through which if any file has not been made due to our program. So, it makes our file and then after that it performs the input and output operations in it. So, first of all, the first thing that we will need to handle any file, what is that? That is our file pointer and here what we will do is we will make a file pointer named FP. So now we are we have made our FP file by pointer. After making the file pointer, what is the first thing that we have to do before using any file, what we have to do is we have to open it and that's why we are going to open it over here. So, you can see here that we are going to use “my file. txt” name file and we have opened it with the help of FP pointer and we have opened it in right mode. Now we know about the right mode, if a file name in this is not already existing. So, the right mode will create this name file, new and after that it performs the right operations in it. So, as we have seen here, for now there is no file of this file name. in our present directory or in the present folder. So, as soon as we will use the right mode over here, this name’s file will be created in our folder. So, how is created that we will see. But for now, we will go ahead and what we have seen ahead? We have made use of fputs function and inside that we have passed one statement and where is that statement getting stored, it will be stored in our “myfile.txt” file. You can see that how there was no file with us, so we created one and after that we performed write operations with the help of fp file pointer. Now, we have performed the write operation. Now, what we will do, now we have to change the mode. We don’t have to write now, we have to read. So, for that what I will do is I we will close this file pointer, I will close this file and with the help of fclose, we will close it after that, I will open it again before opening it again what will I do is I will make one string because the data inside the file, I have to get it in the program, but to get it in the program we have to store it somewhere or the other. So, I will store it in the text named string, whose size I have given as 300 because here I don't know in “my file.txt” how many data is there currently. That's why we have made the text string’s sizes 300. Now, we will go ahead and open our file with the help of fopen function. We have opened the fopen function in read mode because I have to read the data through “my file.txt”, what will be used to read it, fgets function will be used and we can see that here in the printf function we have written that data from the file is in front of it what we have done is, we have made use of fgets function. And you can see that our text string, we have written it over here and along with that we have also passed this file pointer. So, after passing all this information what will happen? Whichever text was written in our file, that will come and get stored through our gets function inside text string. So, in text string has come data from “myfile.txt”. Now what we have to do is we have to again close this file. What will I do to close it, I will make use of fclose function. So, quickly we will do fclose and I have closed fclose to fp file pointer, which means “my file.txt” file. Now we have closed it and we have written the data inside it, we have even read the data. Now is the time of what? To run this entire program and to see is our file creating over here or not? If it is happening then are we able to write the data in it or we are not able. Even after writing are we able to retrieve the data in our program. That is what we will be checking over here. So, what will I do for it is that I will run this entire program but before that we will again see, do we have any file name “my file.txt” inside this folder. So, you can see that we don't have any file of this name in the folder. Now we will go and run this program. So, I will run it over here……. So, you can see that this has successfully run, its output that is coming on our output screen is data from the file “Hello Learnvern”. So, this is the data which has been stored in the file. So, we have read it, where did we read it from even that we will see since everything worked successfully that's why you will see that we have here “myfile.txt” named file has been created. I have not gone personally and created this file, when I run the program, since we have “my file.txt” named file did not exist already. And I had opened it in write mode, that's why this file got created by my write mode and as soon as I open it you can see that here that statement has got printed “Hello Learnvern”. And here we got it printed in our terminal. So, in this way, what we can do is we can perform input and output operations over our file and we can use our other functions, which are used to handle any file. So, this was about file input and output operations, which means read and write operations. This entire program, we quickly completed it. We even created the file, on that we wrote the things and we even read it in our program later.
If you think that there is any part of this video or this topic, you might have not understood then you can ask us any query or question related to that topic without any inhibition.
You just have to go on forums.learnvern.com, you have to type
your query there in your own
language and we will come up with the solution or the answer as soon as possible in your own language.
Apart from this if you wish to have a discussion on any topic. Even that you will be able to do on forums.learnvern.com.
Till that time thank you so much
Share a personalized message with your friends.