delete words from file

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
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.
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
here's a little experimentation which i suspect maybe wrong.It accepts input but the first word gets deleted instead of the specified word
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",ios::ate);
           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');
}
What you can do is similar to searchf(). Instead of cout<<"Word found"<<endl;... you overwrite the word with spaces. Use tellg() and seekp() for the right position to write. Just write as many spaces as the word length. Thus you won't find it later. Use fstream.

See:
http://www.cplusplus.com/reference/fstream/fstream/?kw=fstream
closed account (48T7M4Gy)
Here's one way of thinking about it, which is fairly simple. Read the file in as a single string and use the standard string functions to find and erase the word in question.

It's a start only, you need to add a few extra lines to continue the search beyond the first occurrence and to save the string back (overwrite) to the original file. Case sensitivity isn't addressed either.

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
#include <iostream>
#include <fstream>
#include <string>

int main () {
    std::string file_contents = "";
    std::ifstream myfile ("source.txt");
    
    char ch;
    
    if (myfile.is_open())
    {
        // READ FILE CONTENTS AS STRING
        while ( myfile >> std::noskipws >> ch)
        {
            file_contents += ch;
        }
        
        // DISPLAY STRING
        std::cout << file_contents << '\n';
        
        // GET WORD TO BE DELETED
        std::string word;
        std::cout << "Please enter word to be deleted: ";
        std::cin >> word;
        
        std::string::size_type found;
        
        //DELETE WORD FROM STRING
        found = file_contents.find(word);
        
        if (found!=std::string::npos)
        {
            std::cout << word << " found at: " << found << '\n';
            file_contents.erase(found, word.length());
        }
        
        std::cout << file_contents << '\n';
        
        myfile.close();
    }
    
    else std::cout << "Unable to open file";
    
    return 0;
}
Topic archived. No new replies allowed.