Hello everyone my name is Atharva and we are continuing with this C programming course.
In the last video we had discussed how we can perform input and output operations on one file, which means we can write anything on a text file or we can get any content or text in our program from there.
So, we had seen how these operations can be performed, in the previous video.
So, this video is going to be completely practical based, here we will not understand any theory.
Here we will directly go into our text editor, in visual Studio code and there we will see one code, one program where we are handling one file and how the functions like F open and F close and other functions, how do they perform that is what we will be seeing in this video.
So, without any delay, we will start and we will directly come to our text editor, this is one extremely simple program, where we want to write something on our file, we want to input some data in it.
So, how will those things happen? Come, let’s see.
First of all we have written a few preprocessor commands, where we are importing standard input, output dot H file and along with that standard lib dot H file also we are importing with the help of the preprocessor commands.
After preprocessor commands now comes the time of our main function.
So, now we have come into our main function and from here the step starts about how to handle the file.
So, here we will make one “num” named variable, okay.
Which I don't feel is necessary for now.
I can even remove it from here because we have already made one string.
So here I have removed that variable.
Here the first part that we are seeing in the program, that is the part of making the file pointer.
So, we will see how one pointer is made, whichever variable we are making if we put one asterisk mark in front of it, so it becomes a pointer.
So, here the data type that is going to be of that pointer is of the file type.
Since we are handling the file over here that’s why the data type of this pointer will always be file and we will write this data type in capital letters.
Which means F I L E will always be in capital.
And here we are making one pointer, which we have named for now, F P T R, which is in file data type.
Why have we made it, we will see it going ahead.
So, why I have made this file pointer, file pointer will basically help us which means, it will help our C program, any file that we will be talking about going ahead, to access that.
So, we have seen that we store pointer location and addresses, so where is that file stored and how it has to be accessed, all those things would be stored in our file pointer because it has a different data type.
So, all these things come into this by default that how it has to be stored.
So, we have seen a procedure to make the file pointer, we saw that we have to write the file in capital.
Going ahead we can give any name to the file pointer.
So, this was about the file pointer.
Now, we will go ahead and see here, we have made one string in the name of ST.
How did we get to know that this is a string? Because we have kept the data type as char and the parameters that are there, which means in the square bracket, what we have written is, 100.
What is this 100? 100 is basically the size of the string.
So, how many characters are basically going to come in this, it is telling us that.
So, even this we have cleared and after that now, we are going to open our file with the help of this command.
Before this we will understand one more thing, whenever we have to write this line, which means from where to open the program or where is our file going to get stored.
If you're using Mac OS or Linux, you will have to give the complete path, only then your program runs successfully.
Here since we're working on Windows, we don't need to give the entire path and as it is our file is inside the folder where our program is stored.
So, we are not going to face any such problem.
We will start, how to open this file.
For now, we will not see the left part, we will only focus on the right part.
You can see on the right part, we have used the F open function about which we had discussed earlier that to open one file, this function is used.
Now there are two parameters that come inside of it.
What is the first parameter, it is our file’s name and its location basically.
Here our file’s name is “program dot T X T” and this is completely in double quotes.
Okay, so here we are handling the file named “program dot T X T”.
We even know that when we are using F open function, whichever file name we have given in that, if a file exists of that name, it will open and give us the option of doing the input or output.
But if the file of that name doesn't exist at all, even after that, it will first make the file of that name and it will give us the access to do these operations.
So, in this way, whatever we write our program will open and give it to us.
So, the “program dot T X T” file that is there, we have basically already created it.
So, here our compiler or the F open function is not telling our compiler to make a new file.
So, this was about our first parameter in F open function.
Where we have given one file’s name, we have also given the file’s location, since our file is in the same folder where my program is saved, that's the reason why there is no requirement to write the further path.
But, if you're using this in Mac OS or Linux, then you will have to write the entire path, even if you are there in that particular folder.
So, this was about our first parameter, if we talk about our second parameter, that is our mode.
In this particular program, since we are going to write something on our file, we will be printing something on that file.
So, that's why we are doing it? We are opening it in the write mode which means we are opening it in W mode.
So, this was about our F open function.
After this basically, what we are going to do is with the help of printf statement, we will take a string from our user as an input and with the help of Get S function, which we had learned before.
We will store it in ST string.
So, these two lines we have already seen, this is clear in our mind.
Now we will see our F put S function, how does it work? F put S helps us in writing in one file and basically it handles the strings.
F put char handles the character, it handles single characters.
Here basically it puts the entire string in the file.
So here it's usability increases.
So, if we talk about what its parameters are saying.
So, the first parameter that is there in, it is string.
Which we have to write in our file, which we have to put in the file.
After that if we put a comma and write another name.
So that will be our file pointer’s name.
This file pointer we have written even on the top.
So, you can see that F open, we have basically made it into the file pointer.
So whatever conversation is happening, it is happening through point file pointer.
So whatever conversation is happening, it goes through the file pointer.
Whatever things we tell our file, it goes through the file pointer and gets conversed.
That's the reason why we have stored it in a file pointer.
So here whenever we are using the file pointer it is referring to this file.
And after this, the compiler is able to understand that we are talking about this file.
So, in F put S, we have first written our string and after that we have written the name of our file pointer and this F put S is writing the data on a file very easily.
Now, after this we will come to our F close function.
Since, we have F open our file here, now, we will even have to close it.
Since we have opened it, it is our duty to close it.
So, that's why we will have to close it now, and the way to close it is, by using the F close function and passing its parameter in our file pointer.
So, this is one simple program, where we are writing some data in our file.
So, this is a simple program.
Now, before running it, we will see one more thing, this program dot txt file, what is it? Is it empty or is it full? Or is its location correct or not? So, all these things we will quickly see once.
So, now we will go into the folder.
So, this is our folder where all the other programs are also stored.
So, this is a program on which we are working on right now.
Even that is stored in this folder.
You can see our program dot TXT file is stored over here.
So, we will open it, it will open in the notepad and you can see here that this is completely blank, it is empty.
There is no text in this.
So, we have confirmed that our file exists but there is no data in it.
So, now is our work of program, now we will have to run it.
So, here I have run the program and it is again asking for a string.
So, I will write here, “Hello… Learn…vern”', this is a simple string statement that I have written, “Hello Learnver”.
As soon as I press enter, you can see that the program has successfully completed.
And how will we check it or how can we confirm it that whatever was the process of writing the function, whatever we had passed in the string, whether it has been written on the file or not.
So, we can easily check it again by going in the same folder, by opening the same file.
Have there been any changes in this file? As soon as we open this file, you can see here that “Hello Learnvern”, this entire string has come in written or is printed.
So, in this way, you can see that, we have written “Hello Learnvern”, it was in our C program but where has it come, it has come in our file.
So, in this way our file handling process works.
So, you must have understood here how we have to use the file handling functions here.
How we have to open and close the file.
How do we have to utilise the modes? Along with that the functions that are there, which helps us in providing the input and output on our file.
In Write and Read modes, in that write and read functions which makes these facilities available.
We can explore all these in the future easily.
Here we have seen a basic flow, a basic example, with which we understand that this is a part of file handling flow and how it works.
If you have any queries or comments, click the discussion button below the video and post there. This way, you will be able to connect to fellow learners and discuss the course. Also, Our Team will try to solve your query.
In the next video, we would be discussing the preprocessor part.
Which we used to see how preprocessor commands are there and whatever files that are passed in it.
Which we call as header files.
So, how do we import these header files?
This entire work and process and why are they working in this way.
Why do we write it on the top and not at the bottom of the program? So, all these things we would be discussing in the next video.
Till that time, thank you so much.
Share a personalized message with your friends.