Aug 31, 2017 at 7:30am Aug 31, 2017 at 7:30am UTC
How do i delete words from file.Here is my code.Lines 52-67 are the lines that i need help with.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
#include<iostream>
#include<stdlib.h>
#include<fstream>
#include<string>
using namespace std;
char line[80];
void write()
{
char ch;
ofstream w1;
w1.open("text.txt" );
do
{
cout<<"Enter line(dot to terminate)" <<endl;
cin.getline(line,80,'.' );
w1<<line;
cout<<"More lines?(y/n)" ;cin>>ch;cout<<endl;
}while (ch=='y' );
w1.close();
}
void read()
{
ifstream r1;
r1.open("text.txt" );
while (!r1.eof())
{
r1>>line;
cout<<line<<endl;
}
r1.close();
}
void searchf()
{
string line;
string s;
ifstream f1;
bool found=false ;
cout<<"Enter line to search" ;cin>>line;
cout<<endl;
f1.open("text.txt" );
while (f1>>s)
{
if (s==line)
{cout<<"Word found" <<endl;
found=true ;}
}
if (found==false )
cout<<"Not found" <<endl;
f1.close();
}
void deletef()
{
string s;
string line;
ifstream r1;
cout<<"Enter word to delete" <<endl;
r1.open("text.txt" );
while (r1>>s)
{
if (s==line)
remove(s);
}
r1.close();
}
int main()
{
int n;
char ch;
do
{
cout<<"1-write file" <<endl;
cout<<"2-read file" <<endl;
cout<<"3-search file" <<endl;
cout<<"4-delete words from file" <<endl;
cout<<"Enter option:" ;cin>>n;cout<<endl;
if (n==1)
write();
else if (n==2)
read();
else if (n==3)
searchf();
else if (n==4)
deletef();
else
cout<<"Enter correct option!!" <<endl;
cout<<"Do you want to continue(y/n)?" ;cin>>ch;
cout<<endl;
}while (ch=='y' );
}
Last edited on Aug 31, 2017 at 9:12am Aug 31, 2017 at 9:12am UTC
Aug 31, 2017 at 9:09am Aug 31, 2017 at 9:09am UTC
You make a complete copy of the original file. As you copy each word, if it's the word you want to delete, don't copy it. When done, delete the original file and rename the copy.
Aug 31, 2017 at 10:15am Aug 31, 2017 at 10:15am UTC
what you tell is too complicated for school level exam.
heres what i tried.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
#include<iostream>
#include<stdlib.h>
#include<fstream>
#include<string.h>
using namespace std;
char line[80];
void write()
{
char ch;
ofstream w1;
w1.open("text.txt" );
do
{
cout<<"Enter line(dot to terminate)" <<endl;
cin.getline(line,80,'.' );
w1<<line;
cout<<"More lines?(y/n)" ;cin>>ch;cout<<endl;
}while (ch=='y' );
w1.close();
}
void read()
{
ifstream r1;
r1.open("text.txt" );
while (!r1.eof())
{
r1>>line;
cout<<line<<endl;
}
r1.close();
}
void searchf()
{
string line;
string s;
ifstream f1;
bool found=false ;
cout<<"Enter line to search" ;cin>>line;
cout<<endl;
f1.open("text.txt" );
while (f1>>s)
{
if (s==line)
{cout<<"Word found" <<endl;
found=true ;}
}
if (found==false )
cout<<"Not found" <<endl;
f1.close();
}
void deletef()
{
string s;
string line;
ofstream q1;
cout<<"Enter word to delete" <<endl;
cin>>line;
q1.open("text.txt" );
line=" " ;
q1.close();
}
int main()
{
int n;
char ch;
do
{
cout<<"1-write file" <<endl;
cout<<"2-read file" <<endl;
cout<<"3-search file" <<endl;
cout<<"4-delete words from file" <<endl;
cout<<"5-modify file" <<endl;
cout<<"Enter option:" ;cin>>n;cout<<endl;
if (n==1)
write();
else if (n==2)
read();
else if (n==3)
searchf();
else if (n==4)
deletef();
else
cout<<"Enter correct option!!" <<endl;
cout<<"Do you want to continue(y/n)?" ;cin>>ch;
cout<<endl;
}while (ch=='y' );
}
The delete function accepts input but the file doesnt delete its word.
Last edited on Aug 31, 2017 at 10:17am Aug 31, 2017 at 10:17am UTC