deletion in file handling

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
void deleter()
{
ifstream fd;
ofstream ft;
fd.open("employee.dat");
ft.open("temp.dat");
int flagd=0;
char conf='y';
employee ed;
cout<<"enter employee number to delete:";
int enodel; cin>>enodel;
while(!fd.eof())
{
fd.read((char*)&ed,sizeof(ed));
if(ed.retempno()==enodel)
{
flagd=1;
cout<<"record preparing for delete";
ed.outdata();
cout<<"do you want do delete this record(y/n):";
cin>>conf;
if(conf=='n')
ft.write((char*)&ed,sizeof(ed));
}
ft.write((char*)&ed,sizeof(ed));
}
if(flagd==0)
cout<<"specified employee not found ";  fd.close(); ft.close();
remove("employee.dat");
rename("temp.dat","employee.dat");
cout<<"process complete";
}

]

Could some one tell me what's wrong with my deletion code
thanks
Last edited on
http://www.cplusplus.com/forum/articles/40071/#msg218019
Also, indent and make your testcase self-contained.
Last edited on
ok this was a part of a bigger program i made for the school project
http://www.cplusplus.com/forum/beginner/114905/
1.The program is running properly but the delete code seems to have no effect.That is the even after delete function is run the record is not deleted and it can be searched again.
2.while searching the data string const is not outputted fully
eg:- if
Rahul 
be a name that I inputted for data member name but it displays back
ahul
only in the required space etc
3.The salary also seems to be in some exponential form
All the rest seems to be fine
please help
cheers


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
212
213
214
215
// here is my full corrected code again
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
class employee
 {
private:
int empno,age;
char sex,name[25],desig[10],addr[30],city[15];
float msal,ysal;
public:
void indata();
void outdata();
void modify();
int retempno();
};
void employee::indata()
{
cout<<"enter employee number:";
cin>>empno;
cout<<"enter employee name:";
gets(name);
cout<<"enter age:";
cin>>age;
cout<<"enter sex:";
cin>>sex;
cout<<"enter city:";
gets(city);
cout<<"enter address:";
gets(addr);
cout<<"enter designation:";
gets(desig);
cout<<"enter monthly salary:";
cin>>msal;
}
void employee::outdata()
{
cout<<"\nemployee number:"<<empno;
cout<<"\nemployee name:"<<name;
cout<<"\neage:"<<age;
cout<<"\nsex:"<<sex;
cout<<"\ncity:"<<city;
cout<<"\naddress:"<<addr;
cout<<"\ndesignation:"<<desig;
cout<<"\nmonthly salary:"<<msal;
}
int employee::retempno()
{
return empno;
}

void employee::modify()
{

cout<<"\nemployee number:"<<empno;
cout<<"\nemployee name:"<<name;
cout<<"\neage:"<<age;
cout<<"\nsex:"<<sex;
cout<<"\ncity:"<<city;
cout<<"\naddress:"<<addr;
cout<<"\ndesignation:"<<desig;
cout<<"\nmonthly salary:"<<msal;

int mempno,mage;
char msex,mname[25],mdesig[10],maddr[30],mcity[15];
float mmsal,mysal;

cout<<"enter new employee number(enter '-1' to retain old one):";
cin>>mempno;
cout<<"enter new  employee name:(enter . to retain old one):";
gets(mname);
cout<<"enter new  age(enter '-1' to retain old one):";
cin>>mage;
cout<<"enter new sex(enter '.' to retain old one):";
cin>>msex;
cout<<"enter new city(enter '.' to retain old one):";
gets(mcity);
cout<<"enter new address(enter '.' to retain old one):";
gets(maddr);
cout<<"enter new designation(enter '.' to retain old one):";
gets(mdesig);
cout<<"enter new monthly salary(enter '-1.0' to retain old one):";
cin>>mmsal;

if(strcmp(mname,".")!=0)
strcpy(name,mname);
if(strcmp(mdesig,".")!=0)
strcpy(desig,mdesig);
if(strcmp(mcity,".")!=0)
strcpy(city,mcity);
if(strcmp(maddr,".")!=0)
strcpy(addr,maddr);
if(msex!='.')
sex=msex;
if(mempno!=-1)
empno=mempno;
if(mmsal!=-1)
mmsal=msal;
ysal=(12.00*msal);
}



void add()
{
fstream fa; employee ea;
fa.open("employee.dat",ios::in|ios::app);
char ch;
do{
ea.indata();
fa.write((char*)&ea,sizeof(ea));
cout<<"do you want to add more records(y/n):";
cin>>ch;
}while(ch=='y'||ch=='Y');  fa.close();
}

void deleter()
{
ifstream fd;
ofstream ft;
fd.open("employee.dat");
ft.open("temp.dat");
int flagd=0;
char conf='y';
employee ed;
cout<<"enter employee number to delete:";
int enodel; cin>>enodel;
while(!fd.eof())
{
fd.read((char*)&ed,sizeof(ed));
if(ed.retempno()==enodel)
{
flagd=1;
cout<<"record preparing for delete";
ed.outdata();
cout<<"do you want do delete this record(y/n):";
cin>>conf;
if(conf=='n')
ft.write((char*)&ed,sizeof(ed));
}
ft.write((char*)&ed,sizeof(ed));
}
if(flagd==0)
cout<<"specified employee not found ";
remove("employee.dat");
rename("temp.dat","employee.dat");
cout<<"process complete";
fd.close(); ft.close();
}
void main()
{
label:
clrscr();
employee e; int ch;int flag=0,flags=0;
cout<<"\n1.add record\n2.delete a record\n3.search for a record\n4.modify a record\n5.exit\nenter choice:";
cin>>ch;
if(ch==1)
{
add();
goto label;
}
else if(ch==2)
{
deleter();
goto label;
}
else if(ch==3)
{
cout<<"enter employee no to search:"; 
int empnoms; cin>>empnoms;
ifstream fs;
fs.open("employee.dat");
while(!fs.eof()){
fs.read((char*)&e,sizeof(e));
if(e.retempno()==empnoms)
{
flags=1;
cout<<"record found";
 fs.close();
}
if(flags==0)
cout<<"record not found";
}goto label;
}
else if(ch==4)
{
cout<<"enter employee no to modify:"; 
int empnom; cin>>empnom;
ifstream f;
f.open("employee.dat");
while(!f.eof()){
f.read((char*)&e,sizeof(e));
if(e.retempno()==empnom)
{
flag=1;
cout<<"record found";
e.modify(); f.close();
}
if(flag==0)
cout<<"record not found";
}goto label;
}
else if(ch==5)
exit(0);
else
cout<<"wrong choice";

getch();
}



Last edited on
indent your code
i will i will if u cld help me in this pls .I tried all i can
On line 142 the record is written or not depending on the user input.
On line 144 the record is always written.

This way the question is whether the record exists once or twice. I'd guess that there's an else missing before line 144?

By the way: goto makes it nearly impossible to follow the program flow
the code creates a temp file that copies all the values in employee.dat and except one to be deleted and asks the user to confirm the deletion.I didint teh get the use of else statement before line 144
Well, remove the whole if statement beginning on line 134:
1
2
3
4
5
while(!fd.eof())
{
fd.read((char*)&ed,sizeof(ed));
ft.write((char*)&ed,sizeof(ed));
}
You see what remains? ed is written in any case. Therefore I'd think that
1
2
3
4
5
6
7
8
9
10
while(!fd.eof())
{
fd.read((char*)&ed,sizeof(ed));
if(ed.retempno()==enodel)
{
...
}
else // now write happens only if it is not the "employee number to delete"
ft.write((char*)&ed,sizeof(ed));
}


[EDIT]
And please do what ne555 told you
Last edited on
thanks man but there seems to be still one more query in my mind.what is the best substitute for goto.I seems to have a problem with them also my qno
2.while searching the data string const is not outputted fully
eg:- if
rahul

be a name that I inputted for data member name but it displays back
ahul

only in the required space etc .Sometimes it displays back some AASCI characters
is that a problem with my compiler
pls help
cyber dude
Last edited on
what is the best substitute for goto
A loop

2.while searching the data string const is not outputted fully
what are the steps to reproduce that behavior?
what are the steps dint get your question .This is the worst project I have ever made one or other mistakes evolves every time I TRIES TO MODIFY THIS PROGRAM.THE SEARCH IS NOT WORKING PROPERLY NOW .If there is more than one record it returns back only the first one the rest are not found and even if I ENTER THE CORRECT EMPNO value it returns
specifed employee not found
Last edited on
Topic archived. No new replies allowed.