DELETION CONTINUED
In this blog we are going to see the further implementation of the copying function and how we can use it to perform the deletion operation.
top=-1;
fstream file;
file.open("cp.txt");
Now we check if the file has been successfully opened or not and then we proceed further.For this we use the function file.is_open() function which returns true for successful opening of the file and false for any error in opening the file.
if(file.is_open())
{
cout<<endl<<"file opened successfully\n";
while(file.good())
{
getline(file,pushkar);
}
Further we start reading the data from the file and start storing it in the yash named array in the respective class members.We keep reading the content from the file till we reach end of file.This can be ensured by using the good function.
Let us see what the good function does:-
good function in file handling is used to detect errors while reading/writing in the file.It basically gives us the check of the 3 bits/flags that are:-
1.Fail bit/flag-When there is logical error in any of the i/o operation this bit is set high.
2.bad bit/flag-When there is reading/writing error in i/o operation this bit is set high.
3.eof bit/flag-When the file pointer reaches end of file this bit is set high.
When any of the above mentioned bits are set high then the good function returns false value and we can prevent false reading
of the data.
Now while deletion operation we call the copying function with the argument 0 like copying(0).So this call ensures that copying function will now be performed with slight modification for the deletion operation as respective flags would be set.For deletion we take the name of the author and name of the book and as soon as we encounter this pair while traversing the file the book is omitted from copying.
int g=0;
string alpha,beta;
if(t==0)
{
cout<<endl<<"\n Enter the name of the book ";
cin>>alpha;
cout<<"Enter the name of the author ";
cin>>beta;
}
Now finally we write this data back into the file with the required book deleted and hence we can ensure successful deletion.
Let us see the snippet of the deletion operation that we perform.
In this snippet we have displayed the list of the available books and we are going to select any one of the book for deletion.
Let's select the book with the name upanishads and the author aditya raj and enter it in the respective code.
Below is the required snippet after performing the deletion operation.
Here we can see that the last book named upanishads is deleted.
So we have successfully implemented and performed the deletion operation.Moving forward we will move on to the remaining functions.
WRITTEN BY-
PUSHKAR SADAPHAL(L 59)
Email id-pushkar.sadaphal17@vit.edu
Nice approach and good explanation .
ReplyDeleteGreat
ReplyDeleteGreat
ReplyDeleteGreat
ReplyDeleteGood work!
ReplyDeleteThanks bro
ReplyDeleteGreat
ReplyDeleteGooood going guys
ReplyDeleteEasy and understandable!
ReplyDeleteExcellent
ReplyDeleteGood work
ReplyDelete