Hello everyone, my name is (name) and we are continuing with this C programming course, in which in the last video, we discussed how our devices like our monitor or keyboard. How does our C program communicate with them.
How a C program can converse and how it accesses them.
All these things we had discussed in the last video, along with that if we talk about get S and put S function, so they handle a string and they print the string as an output or they consider it as an input.
After discussing all these pointers and functions, now we are going to continue with this input and output concept, even in this video but here we will be considering it in a different way.
Here we will be talking about the files, the different types of files that are stored in our computer.
So how we can connect with our C program and after that connection, how we can put any input on those files or perform any output operations.
All these things we will be discussing in this video.
So, we will go ahead and see if these input and output functions will be handled on files.
If we talk about a simple file, “a file represents a sequence of bytes.”
So, we all know that in our computer the storage that is there, it is basically stored in bytes form, so when we say that in our computer there is 500 GB of storage or one terabyte of storage is there.
So, here we are talking about B, we call it ‘byte.’
This byte is basically a small storage part.
And these bytes are combined together and are called gigabytes, terabytes or megabytes.
So, in the same way, if we talk about any file, what are these files? It's a sequence or group of bytes which after combining one file is made.
In the same way, these bytes are sequences of files.
If we talk about the C program over here, which type of files it would be handling.
It will be handling two types of files, in which the first file is our ‘text file’.
We all know that a text file has a simple text in it, which means English characters or symbols or numbers. Along with that, the second file which is there, which will be handled by the C program, is the binary file.
So, what is this binary file? Even in this the text is stored, but what is the main difference in it from the normal text file? The characters and the text in it are basically stored in the binary language.
We all know what binary language is? It is a sequence of ones and zeros.
So, whatever is our text, if we convert them into ones and zeros form, it will be very complex for us to understand.
With this, what happens is that our file becomes very secure and no one can copy it easily.
So, this is the way of considering two files in a C program.
We will see how we can consider these files over here and over them, how we can perform input and output functions.
Now to understand it, we will be taking one example.
How is this example? Let's suppose you are a student and what you wish is to take one smartwatch.
What you will do is you will go to your parents and you will tell them that you want a smartwatch.
Now what will the parents do? They will say that we already have a smartwatch.
Let's say that your father has one smartwatch or your mother has one smartwatch.
So, parents usually will give their smartwatch to you.
Right? But you will say that I don't want the old smartwatch, I want a new smartwatch.
So, what they will do is, they will buy a new smartwatch for you and give it to you.
So, in this way, our flow of buying a smartwatch will continue.
If you talk about the files, even the files work in this way.
We will see here what we will do.
So, what is a file? File will be a normal dot TXT extension file, in which there is text.
So, we have connected that file.
Suppose, we have made a file pointer.
So, whatever we had discussed in the previous video, the way we were able to access the devices with the pointers.
Even here we are accessing them with a file pointer and how to write them, even though we will know practically but for now, we will understand that we have to access one file and what we did to access that file? We have made one file pointer and through a file pointer we accessed that file.
Now after accessing the file, we will open it or we will perform more functions on it.
Now we will compare it with our smartwatch example.
Here you will see that we want a file.
Okay? Even we want a file.
So, what will our C program do? Suppose we have given one file’s name like you said that you want a smartwatch.
In the same way, what we did is we gave one file’s name to our C program.
So, what did our C program do? It checked whether we have that file with us already.
On whichever location we wish to keep that file, is that file already there on that location.
If it is there, then our C program will get the same file for us.
(05/57)
If it is not there, it will make a new file with the same name and it will open it and give us.
So, in this way the smartwatch example matches here.
If we already have a smart watch, we get the same one, but if it is not there, we will have to purchase the new one and then we will give it to you.
So, in this way the file flow also goes on.
So, let’s suppose that the file has come to us.
Now we have got the smart watch.
Now how is the smart watch, it is packed.
So, what we will have to do is, first of all we will have to open the box of the smart watch.
In the same way, our file has come to us through the file pointer.
But we will have to open it to use it.
So, that is why we will be using our ‘F open’ function.
Which you can see here as a syntax form, which says that “we use F open function to create a new file or to open an existing file.’’
So, like I said, if there is a file, it will open the existing file but if it is not there, then it will make a new file and get it opened for us.
So, this F open function helps us to open the file.
Open in a sense we are able to access the inside content.
It opens it in this way and provides us.
And in this F open function, there are parameters, obviously.
Since it is a function and in the two round brackets, we write this parameter.
And here there are two parameters that come.
The 1st parameter is the file name.
The way we had discussed was that we give the entire path, in which exact folder this file is or where it should be and after that we put a comma and write a mode, in which mode, we want to open that file.
So, we will see this mode concept ahead.
First, we will see what the file name is.
The file name would be like I said, ABC dot T X T.
I want a file of this name.
And wherever would be our C program basically, it considers the folder from there.
But if I want to access the folder from some other location, the file stored over there or if I want to make any file over there.
So, I will give the path to it in the 1st parameter.
So, in this way, our 1st parameter would be our file name in the F open
function.
And after the comma, the second part or the second parameter that would be there, we call that mode.
So, now we have a file, in which we can make both input and output.
We will see further how it is done.
Here we will understand that we have to do the input in the file.
Which means we have to put the thing in it or we have to take some output from the file.
We have to read something from there.
So, these are different functions, from which function we have to use on the file.
Who tells us this? This is told by the open mode or any mode, tell us this.
Here in the name of the mode, we are seeing in the next slide which all modes we will have, in which we can open the file and we can perform the specific functionalities on those files.
So, here with us, we have different modes in C programming to be performed on the file.
The 1st mode that we will be discussing over here is the
R mode, which we call read mode.
Read means we all know that there is a text and we read it.
Here there is one text written, “File can be opened in various modes”.
So, what we do to the text, we read it.
Right? So, this R mode which is there is helping us to read the text in the text file.
And what else does it do? “It opens an existing text file for reading purposes.” So, what happens in this, the file that is already made, it opens that and it gives us an access to read its content.
In the same way there is a W Mode.
What does the W Mode do? It is for writing purposes and it opens a text file for writing.
What is the meaning of writing, the content that is in the text file, if we want to type more content in that, so that we can do this through this write mode on our file.
If the file is not made over here, it doesn't exist.
So, our W mode, which is the write mode, will also make the file and it will provide us the access to write over it.
We will go ahead and see that we have a few more modes, like our W plus, which means W special mode.
What does this mode do? It opens a text file for both reading and writing purposes.
But if I want to do something like this on my file, along with that I'm reading as well and then I am even writing the content on the file, then in which mode will I open it? I will open it in W plus mode.
And what does this basically do? If any file is already existing, whatever content was there in it.
It will remove it and it will work the write functionality right from the start.
We will be able to type on it right from the start.
So, this was about the W plus mode or W plus function.
After that comes the A mode, A mode is an append mode.
“It opens a text file for writing in an appending mode.”
Now what is an appending mode? Like if we have a certain file and there are 10 lines already written on it.
If you're opening that file in append mode, after those 10 lines, the 11th line which would be there, from there we will be able to write it, which means we will be able to write on it.
If we talk about it, then our R plus mode comes.
Read special mode and what does this do? “It opens a text file for both reading and writing purposes.’’
So, here it is simple.
Here if you want to do both the things together, you will open R + mode.
And if you have to do both the things together.
And along with that the already existing file which is there, you have to flush it, truncate it or you have to remove it.
And from the start you have to write the things in it.
At that time, you can use W plus mode.
So, this was about our modes, here we saw that before using any files, we open that file and we have to open that file in which mode, we have to tell that at that time.
So, now we have opened the file, in that we will perform a few functionalities, which are input and output functionalities like we have spoken about read and write.
So, we'll be seeing this going ahead but for now, we will understand that we have opened the file, we performed all these input and output operations in that.
Now the turn comes to close the file, so like we have taken the smartwatch and we have opened that smartwatch.
We removed it from the box and used it for the whole day but at the end of the day, what we did is, it was a new smartwatch and we didn’t want there to be any damage to it.
So, what did we do? We put it in the same box again.
We packed it and we kept it so that the next day when we use it, it is as good as new.
So, in this way, when we are using the files, we open the file once.
We will also have to close them.
For closing, we have got an ‘F close’ function.
So, this fclose function is very simple.
Its syntax is also very simple.
So, we will use this in our practical and we will see how we open one file and then we close it.
So, we have understood the opening and closing concept.
Now we are talking about the centre process, the input and output operation’s process, read and write process, which was there.
How does that work? So, we are talking about writing on a file.
How can we write in one file? We are talking about that, so the simplest function to write strings to stream, which means to the file is F put S.
So, we had talked about it in the last video about some put S and get S function or put char or get char function.
So here, if we are performing the same functions on the file, in front of it, just this F is put and it becomes F put S.
If I want to print any string on my file.
If I want to write it on my file, I will use this F put S function, in the same way, if I want to print any character in my file, I want to write a character, I will be using the F put Char function.
So, for this, we just have to put the letter F before, before put S, get S, put char and get char functions.
And we can perform the functionalities even on the file.
So, in this way we saw F put S, in the same way, there is one more function F put Char, where we can write char.
Now we will go ahead and see how the reading can be done? How can we read the data from the file? So, even this happens with a simple function which is our F get S function.
So, like we were using get S or get char function and we were interacting with the devices.
In the same way, with the F get S and F get Char function, we will interact with our files.
So, we saw all these things, how a file can be opened, closed, or in between open and closing the reading and writing things which are there, how those can be performed with the help of some functions.
So, this was the theoretical concept, how we can perform these operations over the file.
How can we handle this file, how it can be opened and how it can be closed?
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.
So in the further video we will continue this concept and we will see practically by using them in our text editor and get to know how the functionalities keep on changing for different files based on different modes.
Till that time, thank you so much.
Share a personalized message with your friends.