-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileHandling_Employee.cpp
196 lines (183 loc) · 6.63 KB
/
FileHandling_Employee.cpp
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
#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;
class Emp1 {
public:
int empID;
char name[10];
double sal;
char dest[20];
void accept() {
cout << "\nEnter the employee ID: ";
cin >> empID;
cout << "\nEnter the name: ";
cin >> name;
cout << "\nEnter the salary: ";
cin >> sal;
cout << "\nEnter the destination: ";
cin >> dest;
}
void accept2() {
cout << "\n\tEnter the employee ID to modify: ";
cin >> empID;
}
void accept3() {
cout << "\n\tEnter the name to modify: ";
cin >> name;
}
int getID() {
return empID;
}
void show() {
cout << "\n\t" << empID << "\t\t" << name << "\t\t" << sal << "\t\t" << dest;
}
};
int main() {
int i, n, ch, ch1, rec, start, count, add, n1, add2, start2, n2, y, a, b, on, oname, add3, start3, n3, y1, add4, start4, n4;
char name[20], name2[20];
Emp1 e1;
count = 0;
fstream g, f;
do {
cout << "\n====================================MENU====================================";
cout
<< "\n1.Insert the data\n2.Display the data\n3.Search and modify ID\n4.Search and modify name\n5.Search and modify only number\n6.Search and modify only name\n7.Delete a employee Record\n8.Exit\n\tEnter the choice: ";
cin >> ch;
switch (ch) {
case 1:
f.open("Records.txt", ios::out);
x:
e1.accept();
f.write((char *) &e1, (sizeof(e1)));
cout << "\nDo you want to enter more records?\n1.Yes\n2.No";
cin >> ch1;
if (ch1 == 1)
goto x;
else {
f.close();
break;
}
case 2:
f.open("Records.txt", ios::in);
f.read((char *) &e1, (sizeof(e1)));
cout << "\n\tEmp ID\t\tName \t\t Salary \t\t Designation ";
while (f) {
e1.show();
f.read((char *) &e1, (sizeof(e1)));
}
f.close();
break;
case 3:
cout << "\nEnter the ID of the employee: ";
cin >> rec;
f.open("Records.txt", ios::in | ios::out);
f.read((char *) &e1, (sizeof(e1)));
while (f) {
if (rec == e1.empID) {
cout << "\nRecord found.";
add = f.tellg();
f.seekg(0, ios::beg);
start = f.tellg();
n1 = (add - start) / (sizeof(e1));
f.seekp((n1 - 1) * sizeof(e1), ios::beg);
e1.accept();
f.write((char *) &e1, (sizeof(e1)));
f.close();
count++;
break;
}
f.read((char *) &e1, (sizeof(e1)));
}
if (count == 0)
cout << "\nRecord not found.";
f.close();
break;
case 4:
cout << "\nEnter the name of the employee: ";
cin >> name;
f.open("Records.txt", ios::in | ios::out);
f.read((char *) &e1, (sizeof(e1)));
while (f) {
y = (strcmp(name, e1.name));
if (y == 0) {
cout << "\nName found.";
add2 = f.tellg();
f.seekg(0, ios::beg);
start2 = f.tellg();
n2 = (add2 - start2) / (sizeof(e1));
f.seekp((n2 - 1) * sizeof(e1), ios::beg);
e1.accept();
f.write((char *) &e1, (sizeof(e1)));
f.close();
break;
}
f.read((char *) &e1, (sizeof(e1)));
}
break;
case 5:
cout << "\nEnter the ID of the employee: ";
cin >> on;
f.open("Records.txt", ios::in | ios::out);
f.read((char *) &e1, (sizeof(e1)));
while (f) {
if (on == e1.empID) {
cout << "\nNumber found.";
add3 = f.tellg();
f.seekg(0, ios::beg);
start3 = f.tellg();
n3 = (add3 - start3) / (sizeof(e1));
f.seekp((n3 - 1) * (sizeof(e1)), ios::beg);
e1.accept2();
f.write((char *) &e1, (sizeof(e1)));
f.close();
break;
}
f.read((char *) &e1, (sizeof(e1)));
}
break;
case 6:
cout << "\nEnter the name of the employee: ";
cin >> name2;
f.open("Records.txt", ios::in | ios::out);
f.read((char *) &e1, (sizeof(e1)));
while (f) {
y1 = (strcmp(name2, e1.name));
if (y1 == 0) {
cout << "\nName found.";
add4 = f.tellg();
f.seekg(0, ios::beg);
start4 = f.tellg();
n4 = (add4 - start4) / (sizeof(e1));
f.seekp((n4 - 1) * sizeof(e1), ios::beg);
e1.accept3();
f.write((char *) &e1, (sizeof(e1)));
f.close();
break;
}
f.read((char *) &e1, (sizeof(e1)));
}
break;
case 7:
int empID1;
cout << "Enter the ID of employee to delete: ";
cin >> empID1;
f.open("Records.txt", ios::in);
g.open("Temp.txt", ios::out);
f.read((char *) &e1, sizeof(e1));
while (!f.eof()) {
if (e1.getID() != empID1)
g.write((char *) &e1, sizeof(e1));
f.read((char *) &e1, sizeof(e1));
}
cout << "Record deleted." << endl;
f.close();
g.close();
remove("Records.txt");
rename("Temp.txt", "Records.txt");
break;
case 8:
break;
}
} while (ch != 8);
}