Hello everyone my name is Atharva and we are continuing this C programming course.
In the last video,we discussed input and output functions.
We saw how our input and output functions work.
Apart from these, there are basic functions which we will be discussing in this video.
Now in this video, we are going to see a few more such functions which help us to perform any input and output operations in any file.
First, we had seen some basic input and output operations, which we were using in the file and we have also practically used them but in this video, we are going a little more deep and we will see, how we can conduct input and output operations on our file in different ways.
So here without any delay, we will start and the first function which is there, we will see over here, how we can perform input and output operations with that.
So, the first function that we are discussing is called the F print F function.
In its name itself print f comes, with which we understand that we can use it when we have to basically provide some output.
So, this F print F function is very similar to our normal print F function.
What does a print F function do on our terminal or on our console, it helps us print the output.
In the same way, this F print F function helps us to print something or the other on our file.
So, how is it used? Where we can use it, where we cannot use it.
How will be its syntax? All these things we will be exploring practically.
But before going practically, it is very important for us to know it theoretically.
That's why what we will do is first of all, all the functions in this particular video, which we will be discussing.
We will understand all those functions theoretically and after that, we will go on to our practical work, which means on our text editor and we will go there and explore all the functions and execute them.
So here, we have seen the very first function which is called F print F function and what is its basic work, to print some or the output on our files.
So similar to the print F function, where it helps to print formatted output on the file.
So here, our F print F function, is like print F function but instead of printing the output on the terminal, it depends on the output on the file itself.
Now we will move ahead and see our other function.
Our second function’s name is F scan F function.
It is also like the scan F function which we normally use, which fetches input from our terminal.
So here in this case, when we talk about file handling, when we talk about F scan F function, at that time, when does this function helps us, when we want some input from the file and when we are taking that input from the file in our program, in that situation F scan F function is needed.
Similar to scan F function, it helps to consider formatted input from the file.
So, F scan F is also similar to scan f function.
So, instead of interacting with the terminal, it is interacting with our file.
So, this was about the above F print F function as well as F scan F function.
Now we are moving ahead, we will move ahead and see our third function, which is called the rewind function.
Now, this rewind is a little different.
Till now we were listening about a similar type of function but, we have not heard about the rewind function.
We will read its definition, ``it refreshes the file stream and brings it to the start of the file.”
So, we all remember that when we open a file to perform input or output operations on it, at that time, we have to make one file pointer which access elements in the file or whatever the data is in the file.
So, when we are taking the input from the file, this file pointer comes to use.
When we are giving the output on the file.
Even at that time, our file pointer came into use.
So, if I talk about, suppose I have performed some output operations on my file, and output we have done in this way.
Now, our cursor or our file pointer has come to the end of our sentence, in the file.
The file pointer has come at the end of the sentence because we have performed our output operation here.
Now I want the entire data that I have written in the file. I have to access that but my file pointer is at the end of the file? It will not get any data over there.
It will have to first come to the start which means it will have to come to the start of the file and then once it starts to access from there.
So, from there we will start getting our input.
So, to do this thing, the file pointer which is there, it comes at the end or somewhere in between.
If we want that we want to straight away take it to the start.
So whichever input we want to take from the file or we want to give the output at the start, to get that thing done, so for that reason we are using a rewind function.
Now what do we pass in the round brackets? We'll be passing our file pointer’s name.
How we would be using it, even that we will be seeing in the coming slides.
Before that we will be discussing our other functions.
We will move ahead and discuss our F seek function.
Now what is this fseek function? “It brings the file pointer to a specific location in the file”.
Like we spoke about the rewind function, in that we have seen that it takes a file pointer straight away to the start of the file.
But if we talk about the F seek function, and if you talk about such a scenario where we want to take our file pointer at a certain location, we have to take the input just ahead of it or we have to give the output there.
So, to go to that particular location we will be using our F seek function.
So, in this F seek function, which all arguments we are passing and what are the parameters, all these things we'll be discussing when we will be seeing it practically.
So, we spoke about the F seek function.
Let's quickly move ahead and ahead and we will see our F tell function.
So, these are very easy functions.
Now what does the F tell function do? Till now, we have seen that we were able to take the pointer from here to there with the help of different functions.
But then what is this F tell function? It prints the specific location of the file pointer.
Now the file pointer is on which location exactly, It is on which position? How will we know that? This we can know with the help of the F tell function.
So this F tell function basically returns a value and that value basically tells on which location is our file pointer or the file pointer has been located.
So, this is about F tell function and in the same way, we are able to apply our F seek function.
First with ‘F tell’ ,we got to know where the file pointer is.
So, we used the F seek function and we took the file pointer wherever we wanted to take it.
In this way, these functions work interrelatedly.
We will also use them and see but before that we finish our other functions.
Now we will move ahead and discuss the Get W function.
This is also a very simple function.
‘It is used to extract the integer from the file.’
Suppose we have stored one integer in our file, we have written it and that integer I want it in my output.
I want to use it in my program.
So straight away what I will do is I will use get w function.
So, on whichever position will be our file pointer, on that position whichever integer will be there.
That integer will bring me to my program.
So, this is the work of my get W function.
Now we will move ahead and see our ‘put W’ function.
Now what is this putw function, ‘it is used to put an integer to the file.’
Now this is our file and, on the file, whichever location or position we have our file pointer, there I want to print my integer, I want to write it there.
So, here I would be using the put W function.
In the same way, we will move ahead and we will see our last function, which we are discussing in this video which is called F E O F function.
It is also called the ‘file end of the file function.’
So, basically this is the end of the file function.
Now with its name, we are able to understand why it is used.
“It is used to check whether the file pointer has reached the end of the file.”
When we use it, whatever output value that comes, the return value that comes with that, we understand that, now our file pointer has reached the end of the file or not.
Now what is the need for this? Many times it happens that there is a lot of data in our file and we are going on reading it but, we don't know where exactly the file’s data will end, where is the end of the file listed.
That's why what we do is, as and when we are going ahead in the file, we are accessing each and every element and data.
So continuously we are also checking whether it is the end of the file.
So, there we use this FEOF function from which, we understand that this is not the end of the file, so we have to move ahead.
So as soon as the end of the file is located, we terminate our program and the file ends right there.
So, this was about all our functions, how we can perform input and output operations through them.
And along with that how we can change the location or how we can access its position.
All these things, we have discussed in this function.
So, this was theoretical knowledge, now we will come on to our main purpose, which is understanding practically how it works.
Now, without any delay, we will quickly switch to our text editor.
And here we will be seeing all these functions, which we have discussed in this programme.
So here we will see F print F, F scan F, F seek, F tell, get W, put W, all these functions we will be seeing here.
So, this is our program, first of all we will start with our preprocessor command.
The first command is of STDIO dot H.
which we are using because we have to use the print F function along with that, we have imported the STD dot LIB file because we have to handle the file so, that's why this standard library file is also very important.
Along with that, after the processor command we have now come to our main function.
So, from here our main function starts.
Now, you can see that before the main function, what have we done? We have made three strings over here and what are the names of these three? Data one is the name of the first one, data two is the name of the second string and data three is the name of the third string.
Now, how are these strings, how did we get to know that? Because their data type is char and it is made in the structure of an array, that’s why, we say that we have made the strings over here.
So, data1, data 2 and data 3, these are the strings we have made.
Now why have we made that, we will know even that.
What we are going to do is, with the help of a few functions first of all, we will be making one file and over that file, we will be printing some text and sentences.
We will first print it on the file and after that the content that we had printed, the sentence that we had typed, that sentence we will be taking again in our program from that file.
That's why, we are using three different strings over here, it means that we have broken our sentence into three parts and these three parts are getting stored in three different strings.
As and when we will be going in our program, you will understand this thing clearly.
we have to understand that we have made three strings over here.
Now we will go ahead and we will see that we have made a file pointer which is called ‘file name.’
So, we have made a file pointer with the name file name.
Now we will go ahead and we will see with the help of this command that with the help of F open function, we have tried to open the file.
Now what is the file name that has been listed over here.
The file's name is “anything dot TXT”.
anything dot TXTnamed file is there.
Now we wish to open it with the help of F open function and in which mode we want to open it? We want to open it in W plus mode.
Which provides both read and write functionalities.
Here we don't know whether anything dot TXT file actually exists or not.
So once again we will cross check, do we have anything dot TXT name file over here.
As soon as we come near the A alphabet file, you can see that anything dot TXT named any file is not made with us.
This means that what will our program do? First it will check if anything dot TXT named file is made or not.
If it is not made then it creates the file on its own.
This was about the F open function.
Here we are using the file pointer, file name…
Now we will move ahead and we are using a very first function for output, we will be using it.
Here, we want to print something on the file.
As soon as we run the F open function over here, anything dot TXT named file is made because it was not made from early on.
That's why a new file is made.
A new file is obviously empty.
So, we will have to put some data in it.
What is that data? Data, basically we are putting from the F print F function, we just read about it.
What does it do? It helps us to print something in our file, what is the syntax to write it.
The syntax of writing would be, its argument list is divided into two parts, in the first part we write the file pointer’s name.
In this way, we have written the file name which is the file pointer’s name.
After that, you can see the secondary part is the same as the print F function, in which we were writing our format specifier and after that we first wrote our format specifiers.
After that we will write our variable names or whichever data we want to put there.
You can see that here we have written three, %S, %S, %S, format specifiers.
This means that we want to print three strings in the file.
We want to print three strings but what are these three strings? These strings are welcome.. to.. Learnvern.
So, the first string is welcome.
The second string is two and the third string is Learnvern.
So, these three strings, we are basically handing over here that's why, we have used three format specifiers here.
So, here, we are clear about the F print F function.
Now, we will come to our next line.
What have we done here? We have used the ‘rewind function.’
First, let's understand that when we used the F print F function and when we implemented it, executed it.
What happened in it? Our file was created with the F open function.
After that what happened, as soon as we printed the first thing ``welcome, so “welcome” was printed.
Then “to” was printed.
Then “Learnvern '' got printed.
So, how this is getting printed.
That is getting printed one after the other, what does it mean? With this we understand that our file pointer, after printing the entire word, is found at the end.
That's why when we are printing a new string over there, it will be printed from the end.
When it will print from the end.
After printing all the strings, our file pointer must have reached the end of our file.
But now we want the data that we had input here in our file.
Now, we want that we should get that again in our program.
For that we will have to again bring our file pointer at the start.
Then it will access the data one by one and from there we can get the data in our program.
Now to take the file pointer right at the start, which function we will be using? We will be using a rewind function about which we just read.
What does this rewind function do? In that we have to pass the file pointer’s name which is the file name.
From here it will basically take our file pointer straightaway at the start of the file.
Now again our file pointer has come to the start of the file.
Now, we will be using our F scan F function.
Like we were taking the input from the terminal, now our F scan F function will basically take the input from the file.
Now this is a similarity between scan f and f scan f.
The only difference is that our scan f function was interacting with the terminal but the f scan f function was interacting with the file.
You will see that ‘f scan f’ function’s syntax is similar.
In the first pointer we have to write the file pointer’s name and in the secondary part is also the same like our normal scan f function.
We have written three format specifiers and after that, we have passed the three strings over here which we had made at the start of our program.
Now here you must be understanding why we had made the strings, so that when from the files when we are extracting the values again, we are extracting the data.
We have to store it in some or the other place and what is that thing, that is a string.
So, we have put three words here.
So, we are taking it in three different strings.
We are considering it, that's why we have written three data strings here like data one, data two and data three.
Three such strings we have written here.
Whose format specifiers are written as %S, %S and %S.
So, this was about our f print f function, rewind function as well as f scan f function.
Now after this we will come to the ‘F seek’ function.
Now we will see all these functions, how they are written.
Then we will be executing this entire program.
Now, we have come to our ‘f seek’ function.
What does the ‘f seek’ function used to do? It used to keep our file pointer on any location.
So, how will it keep it? How will he know that it has to be kept here? That’s why the parameters and arguments that we're passing in it.
That will tell our compiler that on which particular location we have to keep our file pointer.
So here you can see that the first part of the argument, in that, we are passing the file pointer's name, which is the file name, along with that the second part in the arguments list.
It is basically the integer value that is seen.
What is this value? I will explain it to you after two seconds.
Before that, we will understand the third value over here.
The third value, you can see that 'seek end is written over here, And what does seek end means? First of all, the pointer will keep it at the end.
It will keep right at the end of the file.
As soon as it comes at the end, the centre value which was there, it will depend on it now that the file pointer has come at the end, so now from the file pointers, how many bytes we must go ahead or how many bytes we must go behind.
So that our file pointer gets placed there.
So, in this way, our F seek function works, in the place of ‘seek end’, we can also put something else.
As you can see, the “seek CUR” name’s argument is there that we can write.
Now what it does is, it keeps our pointer on the current position of it.
But how much to do in front and behind, it is told to us by our second argument.
So, here we saw that the 1st seek end, the 3rd argument which we passed
with the name seek end.
It basically takes our file pointer right at the end of the file.
Seek cur, this particular argument takes us to the current file pointer’s location.
Along with that, if we talk about “SEEK SET”, Seek set argument takes our file pointer at the start of the file.
So, here basically, we are taking our pointer at the end.
Why are we taking it? We will see how the value is getting printed.
So, with the F seek function, we want that it should take our file pointer right at the end of the file.
And now time comes for the next function which is called F tell function.
We know what F tell does.
It tells us which position is our file pointer.
F tell function tells us which position is our file pointer.
What have we done for it? In F tell, we have passed the file pointer’s name, whatever value is returned by our F tell function.
That we have assigned and stored in the “FT” named variable.
We have even declared it over here and even initialised the FT variable.
So, in the next line, whichever value is stored in FT.
We will print it here and see with the help of the normal print F function.
So, till now what we did? First of all, we made the file and in the file, the data we wanted to print, that data we got printed.
After that with the F seek function, we saw that there we can keep the file pointer.
So, we kept it right at the end of the file and with the help of F tell function, we're trying to see the exact location and position in the numeric form, how much is that.
That we have stored in FT and now we are printing FT here.
So, this was about the F seek and F tell function.
Now comes the time of one more new function, but what are we doing before that? Whatever data we have taken as input with the help of F scan F function, whatever data we have got from the file to our program, we will be printing it now.
We have made use of print F statements to print that.
And we have printed, “The data from file is” For which we have made three strings.
That we will print here with the help of format specifiers.
After doing all these things, we will close the file with the help of F close function.
And in that, we will write our file pointer’s name.
As and when we are going ahead, going ahead we are opening one file once again and it is no other file, it is the same file which is called “anything dot TXT”.
So, now we have a file name, with the same file pointer’s name, we are again opening the file but what will happen this time, this file will not become new again.
Why? Because it already exists.
First time we opened it, it was made at that time.
But the second time when we opened it, it was already made.
It will open the already made file and give it to us.
So the file has been opened now with the name of “anything dot txt”.
Now, on whichever location file pointer is there, at that location we will insert one integer.
So, how we will be able to insert it, with our put W function.
In this you, can see that in the first, after putting a comma, we have written our file pointer’s name ovet argument we had passed our integer, which we have to basically input in our file.
Along with that here.
So, this was about our Put W function.
We have put the integer in our file.
Now we will have to rewind it again, because our file pointer has come to the next step.
So, we have to again put it at the start of our file so that when we're extracting the integer, we should get the exact integer.
So, that’s why we have used the rewind function here again.
And after the rewind function, we used the get W function.
Now we know it and we have also learned it as well, what this get W function does.
It extracts the integer from the file and gives it to us and we have to store it in any variable.
So, here I have stored the number in the C variable which is of the integer type.
Whichever output or return comes from the get W function.
That gets stored in the C variable.
As soon as it gets stored in C, we will get it printed.
We have used print F statement and in print F statement, we have written, “we get the integer from the file as” and we have used the format specifier and before that, we have written C variable.
After doing all these things, we opened the file once again.
So, we know that once the file is opened, we also have to close it.
That’s why we have put Fclose function over here.
We have passed the file pointer’s name in it.
And our file got closed here.
After doing all these, things our main function ends here.
That's why we have written “return zero”.
Which means the main function will return, which means it will get over here.
This is our entire flow and this has become our program.
We will quickly see which are things we have to check in our output.
First of all, we have to see whether anything dot TXT named file is made.
After that, we have to see whether this particular text has been printed in it.
Now that it is printed, after that we will see our F seek function that was there, when it takes our file pointer at the end.
Then the F tell function, which particular location’s number, numeric value, it prints and gives us.
After printing that value, we will print here the entire data which we had input in the file, which means “Welcome to Learnver”.
And after printing it, we will be closing the file.
After closing the file, we will open the file again and then we will be inserting one integer in that.
That integer would be always getting inserted in the start of the file.
Because when we open a new file, it is at the start, till the time we don’t open it in append mode.
Now this opens at the start of the file.
So, with the help of put W, we are pasting one integer, we are putting it.
After that with the help of get W function, we are getting that integer back in the program.
And here with the help of print F, we're trying to print it here.
So, this is the entire flow of the program.
now we will save it and we will quickly execute it.
To execute that we will have to run the GCC command over here and after that,we will be able to see, our entire functions, which means our entire program,is it running properly?
As soon as we will run it, you will see that some statements are printed and have come to us.
The first thing that we have to see is, whether our “anything dot txt” file was made.
So, we will go to our folder where this particular program is saved.
You will see that anything dot TXT named file has been made here.
So, anything dot TXT named file is made over here with us.
And in this the data that we had entered, is it printing and giving us, that we will check here.
But before that, we will see when we had used the ‘F seek’ function over here and we had taken it right at the end of the file.
So, what happened when we took it at the end of the fine.
When we told F tell that you print it for us on which position is our file pointer.
So, it printed this value for us which is 20.
If I count the entire string that we had printed in our file, which was “Welcome to Learnvern.” If I count each of the characters of it with the spaces then it becomes 20 and that’s why F tell has also told us that our file pointer is on the 20th position, which means it is right at the end of the file.
So, here these two functions are helping us to print the output.
After this we will see that the second line that has got printed is, “The data from file is” and before that you can see that we have got this statement printed, which we had printed before in our file, which is “Welcome to Learnvern”.
If you really count it, you can see that 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 and 20.
So, basically this entire string is of size 20.
So, here we saw with the help of F tell that, it was on 20th position.
That means at the end of the file.
Now we will move ahead as we had inserted one integer.
That was our 4 integer, you can see that here with the help of put W, we inserted 4.
So, did we get 4 as it is? So, “We get the integer from the file as 4, So, 4 we got with the help of put W and get W.
So, in this way, we can use different functions to input and output functions.
Along with that, manipulating the file position.
So, all these things we can do with our file handling.
The file should be handled, there should be input and output on it, its file pointer should also be manipulated.
And whatever things we want to do with my file, all those things we can do with the help of these functions.
So, this was about all the functions.
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 will discuss how our preprocessors work, what exactly is the preprocessor? Then the
preprocessor commands come into vision.What are the preprocessing commands? So, all these things, we would be discussing in the coming module or the coming video.
We will meet you in the next video, till that time, thank you so much.
Share a personalized message with your friends.