How to save and delete some word in the text file in C++ without resetting it?

Every time I open my program the text file becomes empty and how can I save all the inputs in the text file I mean how can I still edit and see my last inputs? And here's my another problem every time I delete a name or an id number this program deletes all my inputs including those are who are not included so how can I delete a word or a text in the text file without affecting the other records, in short how to delete a word specifically?

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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;

int main()
{
	static int totrec=0;    //totrec variable keep track for total variable entered
	{						//Initially Record scanned are Zero void main()
		int choice;
		char name[100];
		while(true)
		{
		cout<<"\n\n\nChoose your choice\nNOTE : one choice for one record(except viewing data)\n";
		cout<<"1) Scanning intial records\n";
		cout<<"2) Add records\n";
		cout<<"3) Modifying or append records\n";
		cout<<"4) View records\n";
		cout<<"5) Search Record\n";
		cout<<"6) Delete Record\n";
		cout<<"0) Exit\n";
		cout<<"Enter your choice : ";
		cin>>choice;
		while(choice<0&&choice>5)
    	{
    		cout<<"\nInvalid Choice\nTRY AGAIN\n"<<endl;
    		cin>>choice;
    	}
    	
    	while(!cin&&cin.fail()) 
		{ 
    		cout<<"\nInvalid Choice\nTRY AGAIN\n"<<endl;
    		cin.clear(); // clear the error 
    		// clear the input stream 
    		cin.ignore(10000,'\n'); 
    		cin>>choice;
		} 
		switch(choice)
		{
			  case 1:  //Scanning intial records
			  {
						ofstream outfile;
						outfile.open("emp.txt",ios::out);
						cout<<"\n\nPlease enter the details as per demanded\n";
						cout<<"\nEnter the name : ";
						cin.ignore();
						cin.getline(name, 100);
						outfile<<name<<endl;
						cout<<"Enter Age : ";
						int age;
						cin>>age;
				        outfile<<age<<endl;
						int id;
						cout<<"ID Number : ";
						cin>>id;
						outfile<<id<<endl;
						cin.ignore();
						totrec= totrec + 1;
						outfile.close();
			  }
			  break;
			  case 2:   //Appending records;
			  {
						ofstream outfile;
						outfile.open("emp.txt",ios::app);
						cout<<"\n\nPlease enter the details as per demanded\n";
						cout<<"\nEnter the name : ";
						cin.ignore();
						cin.getline(name, 100);
						outfile<<name<<endl;
						cout<<"Enter Age : ";
						int age;
						cin>>age;
						outfile<<age<<endl;
						string id;
						cout<<"ID Number : ";
						cin>>id;
						outfile<<id<<endl;
			
						totrec = totrec + 1;
						outfile.close();
			  }
			  break;
			  case 3:   //Modifying or append records
			  {
						ofstream outfile;
						outfile.open("emp.txt",ios::ate);
						cout<<"Are you interested in adding record\nenter y or n\n";
						char ans;
						cin>>ans;
						if(ans=='y' || ans=='Y')
						{
							cout<<"\n\nPlease enter the details as per demanded\n";
							cout<<"\nEnter the name : ";
							char name[20];
							cin>>name;
							outfile<<name<<endl;
							cout<<"Enter Age : ";
							int age;
							cin>>age;
							outfile<<age<<endl;
							string id;
							cout<<"ID Number : ";
							cin>>id;
							outfile<<id<<endl;
							cin.ignore();
							totrec = totrec + 1;
						}
						outfile.close();
			  }
			  break;
			  case 4:  //Viewing records
			  {
						ifstream infile;
						infile.open("emp.txt",ios::in);
						const int size=80;
						char line[size];
						int counter=totrec;
						while(counter > 0)
						{
							infile.getline(line,size);
							cout<<"\n\nNAME     : "<<line<<endl;
							infile.getline(line,size);
							cout<<"AGE      : "<<line<<endl;
							infile.getline(line,size);
							cout<<"ID NUMBER: "<<line<<endl;
							counter--;
						}
						infile.close();
			  }
			  break;
			  case 5:  //search
			  {
						char search[100];
						int offset;
						string line;
						
						ifstream infile;
						infile.open("emp.txt");
						cout<<"Type the name you want to search"<<endl;
						cin.ignore();
						cin.getline(search, 100);
	
						if(infile.is_open())
						{
							if(!infile.eof())
							{
								getline(infile,line);
								if((offset=line.find(search, 0))!=string::npos)
								{
									cout<<search<<" has been found in the text file"<<endl;
								}
								else
								{
									cout<<"No results found!"<<endl;
								}
							}
							infile.close();
						}
						else
						cout<<"Could not open file"<<endl;
			  }
			  break;
			  case 6://delete
			  {
				string line; 
				char deleteline[100];
				ifstream stopword;
				stopword.open("emp.txt");
				if (stopword.is_open())
				{
					if ( getline (stopword,line) )
					{
						cout << line << endl;
					}
					stopword.close();
				}

				else cout << "Unable to open file";

				ofstream temp;
				temp.open("temp.txt");

				cout << "Please input the stop-word you want to delete..\n ";
				cin.ignore();
				cin.getline(deleteline,100);

				if (getline(stopword,line))
				{
					if (line != deleteline)
					{
						temp << line << endl;
					}
				} 
				temp.close();
				stopword.close();
				remove("emp.txt");
				rename("temp.txt","emp.txt");
				cout <<endl<<endl<<endl;
			  }
			  break;
			  case 0  :
			  {			  
					return 0;
			  }
			  default :  cout<<"\nInvalid Choice\nTRY AGAIN\n";
			  }
			}
			
		}
	}
Registered users can post here. Sign in or register to post.