Hi everyone.
So from this lesson we will start learning file handling in C++.
Now in this world we will learn this topic writing to a file.
So for writing to a file the syntax.
What do you have to lose so for writing the code.
First of all you have to include a file and the name of the file is f a stream file stream so after
including the file so it is obvious that we will write the code inside me.
Then what we have to do so the name of the class is o f stream output file the stream.
So this is a class.
And what you have to do is we have to create an object.
Let's say the name of the object is output file.
I would file is the name of the object.
And so what this object will do.
I am calling a constructor so for calling the constructor you have to give a file name so I'm giving
the file in my final dirty.
So suppose there's a file and the name of this file is my file in my file dart the 60 so after writing
this piece of code what will happen.
So there is a connection between this object and this file.
So a connection will get established between this object and this file.
So basically you will do anything with this object.
Then the changes will get reflected inside this file.
So whatever you're going to do with this object the changes are going to be reflected in the file.
My friend not the extreme.
Now there are two things that can happen.
So after writing this line.
So when you will write this line then you will execute this line.
There are two possible situation.
So the first situation is this file already exists.
This file already exists.
So what this statement will do.
So this statement will open the file.
So if the file already exist then open the file then open the file.
Now the second situation is this file does not exist.
So if this file does not exist then this piece of code what will do with this line what it will do.
It will create the file.
So it will create file with the name my file not the 60s.
So I'll finally be created with this name my file not the 60 and then open that file.
So cleared file and then open that file so cleared file and open file.
I'm everything myself.
So this line can do two things.
So if the file already exists.
So if this file already exists then open the fight.
And if this file does not exist then basically cleared the file with this name.
My file not the 60 and then open that file simple.
Now let us discuss next thing.
So what can happen.
So for example this is your file.
My file not the 60.
So basically if you will do something like this Let's say you are trying to write something to a file.
So if you do this out file you want to insert you want to write Hello world inside this file hello world.
And Andy.
So what will happen the effect of this line is go to the file and write Hello world.
So this file can be empty so the first scenario is this file can be empty.
The other scenario is this file list containing some data
so this file might be containing some data so if you execute this piece of code.
So if this is the situation then obviously what will happen.
So Hallowell will get printed here.
Now if this is the situation your file is already containing some data then what this line will do.
So this line will actually truncate.
So this line will actually delete all your data and then it will insert Hallowell so all the data will
be deleted and then this string will get inserted.
Now if you want to preserve the data if you want to preserve your oil they don't you do not want their
oil later to be removed.
So what do you have to do you have to pass of one what barometer in the constructor.
Basically we have so the second parameter can be.
So we'll hear what I'm doing.
I am writing off a stream then the name of the object is out file the name of the file is my file not
the 60.
Now the second barometer can be one of the two things.
So the second parameter can be us.
Column Column D R U N C so D2 and C means truncate.
So this is by default if you will not give the second barometer then this is the second barometer by
default.
And if you want to preserve the content.
So if you want to preserve the content do you have to write something like this.
IOW column Column B so APB stands for append the data so this all thing is Same here.
So there are two types.
So the second parameter can be truncate.
So this is by default.
So I have not written truncate here.
So what this line will do if you want to insert something into the file it will delete your all data
and then it will insert Hallowell.
But if you want to preserve the data if you want to preserve the data then you have to write a US column
column append.
So what will happen if a file is already containing some data then Hallowell will get inserted at the
end.
OK.
So by default this is truncate and if you want to present the data I will use append APB.
So this is just an index for driving data to a file.
Similarly you can write more data.
For example you can write out file.
Let's say you want to insert it in teachers or you can write.
I would file 50 and line similarly.
You can write it like this.
I would file.
Let's say you want to insert the branch of the student so you can insert CSC and line.
So in this way this is this index for writing into a file.
Now after writing to the file after your work is or what.
The most important thing that we have to do is basically close the connection.
So you have to write you have to call the function dot close.
So the name of the function is close.
So after doing all the work that you want to do with the file you have to call the function goes.
So what this close function will do it will.
So this is the connection so this connection will get a break.
This connection will be broken if you will call the function close and it is very important.
It is a very good practice also and it is very important also to call the function close.
Now what will happen if you will not call this function.
So why this function is important why calling of this function is important.
So you have seen many times for example when you connect pen drive to be a computer and then if you
want to eject to a pen drive.
So after connecting the bend over to a computer if you want to eject you up and drive then sometimes
an added pop ups that some program might be using this pen drive.
So you can not eject and that is because of this thing.
So the developed but forgot to write this line output finally got close.
So that's why the connection is still there.
So if the connection is it still did if the connection is still there then you will get this idea that
some program is using your pen drive.
So you cannot eject.
So what you're doing those scenarios you basically lose that program.
So if the program is closed then what will happen.
So this connection will be broken disconnection will break.
And you can eject you up and drive.
So it is always a good practice after your work is done with the file.
You have to call the closed function so in the next we do we will write the code.
I will see in the next one maybe.