-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathers.c
403 lines (372 loc) · 15.1 KB
/
ers.c
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#include <stdio.h> ///for input output functions like printf, scanf
#include <stdlib.h>
#include <string.h> ///string operations
/** Main function started */
int main(){
FILE *fp, *ft, *fd, *fdt; /// file pointers
char another, choice;
int len, recfound=0;
/** structure that represent a department*/
struct dep{
char depid[4]; // dep id
char dname[20]; ///dep name
};
/** structure that represent a employee */
struct emp{
char empid[7]; // employee id
char did[4]; // dep id
char fname[40]; ///first name of employee
char lname[20]; // last name of employee
int age; /// age of employee
float bs; /// basic salary of employee
};
struct emp e; /// structure variable creation
struct dep d;
char empId[7]; /// string to store name of the employee
char depId[7]; /// string to store name of the department
long int recsize; /// size of each record of employee
long int depsize;
/** open the file in binary read and write mode
* if the file EMP.csv already exists then it open that file in read write mode
* if the file doesn't exit it simply create a new copy
*/
fd = fopen("DEP.csv","rb+");
if(fd == NULL){
fd = fopen("DEP.csv","wb+");
if(fd == NULL){
printf("Connot open file");
exit(1);
}
}
fp = fopen("EMP.csv","rb+");
if(fp == NULL){
fp = fopen("EMP.csv","wb+");
if(fp == NULL){
printf("Connot open file");
exit(1);
}
}
/// sizeo of each record i.e. size of structure variable e
recsize = sizeof(e);
depsize = sizeof(d);
/// infinite loop continues untile the break statement encounter
while(1){
mmenu:
system("clear"); ///clear the console window
printf("**************** Employee Management System *************\n");
printf("* *\n");
printf("* 1. Department *\n");
printf("* *\n");
printf("* 2. Employee *\n");
printf("* *\n");
printf("* 3. Exit *\n");
printf("* *\n");
printf("*********************************************************\n\n");
printf("Your Choice: "); /// enter the choice 1, 2, 3
fflush(stdin); /// flush the input buffer
choice = getchar(); /// get the input from keyboard
switch(choice){
case '1': /// if user press 1
system("clear");
/// infinite loop continues untile the break statement encounter
while(1){
system("clear"); ///clear the console window
printf("************************ Department *********************\n");
printf("* *\n");
printf("* 1. Add Record *\n"); /// option for add record
printf("* *\n");
printf("* 2. List Records *\n"); /// option for showing existing record
printf("* *\n");
printf("* 3. Modify Records *\n"); /// option for editing record
printf("* *\n");
printf("* 4. Delete Records *\n"); /// option for deleting record
printf("* *\n");
printf("* 5. Main Menu *\n"); /// exit from the program
printf("* *\n");
printf("*********************************************************\n\n");
printf("Your Choice: "); /// enter the choice 1, 2, 3, 4, 5
fflush(stdin); /// flush the input buffer
choice = getchar(); /// get the input from keyboard
switch(choice){
case '1': /// if user press 1
system("clear");
fseek(fd,0,SEEK_END); /// search the file and move cursor to end of the file
/// here 0 indicates moving 0 distance from the end of the file
another = 'y';
while(another == 'y'){ /// if user want to add another record
did:
printf("\nEnter 3 digits Department ID: ");
scanf("%s",d.depid);
if(strlen(d.depid)>3)
goto did;
printf("\nEnter Department name: ");
scanf("%s",d.dname);
fwrite(&d,depsize,1,fd); /// write the record in the file
printf("\n\nAdd another record(y/n) ");
fflush(stdin);
another = getchar();
}
break;
case '2':
system("clear");
rewind(fd); ///this moves file cursor to start of the file
printf("------------------------------------\n");
printf(" Id | Department Name\n");
printf("------------------------------------\n");
while(fread(&d,depsize,1,fd)==1){ /// read the file and fetch the record one record per fetch
printf("\n");
printf("%s\t| %-10s|",d.depid,d.dname); /// print the name, age and basic salary
}
getchar();
break;
case '3': /// if user press 3 then do editing existing record
system("clear");
rewind(fd); ///this moves file cursor to start of the file
printf("------------------------------------\n");
printf(" Id | Department Name\n");
printf("------------------------------------\n");
while(fread(&d,depsize,1,fd)==1){ /// read the file and fetch the record one record per fetch
printf("\n");
printf("%s\t| %-10s|",d.depid,d.dname); /// print the name, age and basic salary
}
another = 'y';
while(another == 'y'){
printf("\n\nEnter the department id to modify: ");
scanf("%s", depId);
rewind(fd);
while(fread(&d,depsize,1,fd)==1){ /// fetch all record from file
if(strcmp(d.depid,depId) == 0){ ///if entered name matches with that in file
printf("\n\nEnter new department name: ");
scanf("%s",d.dname);
fseek(fd,-depsize,SEEK_CUR); /// move the cursor 1 step back from current position
fwrite(&d,depsize,1,fd); /// override the record
break;
}
}
printf("\nModify another record(y/n)");
fflush(stdin);
another = getchar();
}
break;
case '4':
system("clear");
rewind(fd); ///this moves file cursor to start of the file
printf("------------------------------------\n");
printf(" Id | Department Name\n");
printf("------------------------------------\n");
while(fread(&d,depsize,1,fd)==1){ /// read the file and fetch the record one record per fetch
printf("\n");
printf("%s\t| %-10s|",d.depid,d.dname); /// print the name, age and basic salary
}
another = 'y';
while(another == 'y'){
printf("\n\nEnter id of department to delete: ");
scanf("%s",depId);
fdt = fopen("Dtemp.csv","wb"); /// create a intermediate file for temporary storage
rewind(fd); /// move record to starting of file
while(fread(&d,depsize,1,fd) == 1){ /// read all records from file
if(strcmp(d.depid,depId) != 0){ /// if the entered record match
fwrite(&d,depsize,1,fdt); /// move all records except the one that is to be deleted to temp file
}
}
fclose(fd);
fclose(fdt);
remove("DEP.csv"); /// remove the orginal file
rename("Dtemp.csv","DEP.csv"); /// rename the temp file to original file name
fd = fopen("DEP.csv", "rb+");
printf("Delete another record(y/n)");
fflush(stdin);
another = getchar();
}
break;
case '5':
//fclose(fd); /// close the file
goto mmenu; /// go to Main menu
}
}
//break;
case '2':
/// infinite loop continues untile the break statement encounter
while(1){
system("clear"); ///clear the console window
printf("************************* Employee ***********************\n");
printf("* *\n");
printf("* 1. Add Record *\n"); /// option for add record
printf("* *\n");
printf("* 2. List Records *\n"); /// option for showing existing record
printf("* *\n");
printf("* 3. Modify Records *\n"); /// option for editing record
printf("* *\n");
printf("* 4. Delete Records *\n"); /// option for deleting record
printf("* *\n");
printf("* 5. Main Menu *\n"); /// exit from the program
printf("* *\n");
printf("*********************************************************\n\n");
printf("Your Choice: "); /// enter the choice 1, 2, 3, 4, 5
fflush(stdin); /// flush the input buffer
choice = getchar(); /// get the input from keyboard
switch(choice){
case '1': /// if user press 1
system("clear");
fseek(fp,0,SEEK_END); /// search the file and move cursor to end of the file
/// here 0 indicates moving 0 distance from the end of the file
another = 'y';
while(another == 'y'){ /// if user want to add another record
eid:
printf("\nEnter 6 digits Employee ID: ");
scanf("%s",e.empid);
if(strlen(e.empid)<6)
goto eid;
fdid:
printf("\n\nAssign department id to employee: \n");
rewind(fd); ///this moves file cursor to start of the file
printf("------------------------------------\n");
printf(" Id | Department Name\n");
printf("------------------------------------\n");
while(fread(&d,depsize,1,fd)==1){ /// read the file and fetch the record one record per fetch
printf("\n");
printf("%s\t| %-10s|",d.depid,d.dname); /// print depid and name
}
printf("\n\n");
scanf("%s",e.did);
rewind(fd);
while(fread(&d,depsize,1,fd)==1){ /// fetch all record from file
if(strcmp(e.did,d.depid) == 0){ ///if entered name matches with that in file
recfound = 1;
break;
}
}
if(recfound == 0)
{
printf("\nInvalid department. Please refer to department records\n");
goto fdid;
}
printf("\nEnter First name: ");
scanf("%s",e.fname);
printf("\nEnter Last name: ");
scanf("%s",e.lname);
printf("\nEnter age: ");
scanf("%d", &e.age);
printf("\nEnter basic salary: ");
scanf("%f", &e.bs);
fwrite(&e,recsize,1,fp); /// write the record in the file
printf("\n\nAdd another record(y/n) ");
fflush(stdin);
another = getchar();
}
break;
case '2':
system("clear");
rewind(fp); ///this moves file cursor to start of the file
printf("----------------------------------------------------------------------\n");
printf(" Id | DepId | First Name | Last Name | Age |Basic\n");
printf("----------------------------------------------------------------------\n");
while(fread(&e,recsize,1,fp)==1){ /// read the file and fetch the record one record per fetch
printf("\n");
printf(" %s\t| %s\t| %-15s| %s\t\t| %d\t\t| %.2f",e.empid,e.did,e.fname,e.lname,e.age,e.bs); /// print the name, age and basic salary
}
getchar();
break;
case '3': /// if user press 3 then do editing existing record
system("clear");
rewind(fp); ///this moves file cursor to start of the file
printf("----------------------------------------------------------------------\n");
printf(" Id | DepId | First Name | Last Name | Age |Basic\n");
printf("----------------------------------------------------------------------\n");
while(fread(&e,recsize,1,fp)==1){ /// read the file and fetch the record one record per fetch
printf("\n");
printf(" %s\t| %s\t| %-15s| %s\t\t| %d\t\t| %.2f",e.empid,e.did,e.fname,e.lname,e.age,e.bs); /// print the name, age and basic salary
}
another = 'y';
while(another == 'y'){
printf("\n\nEnter the employee id to modify: ");
scanf("%s", empId);
rewind(fp);
while(fread(&e,recsize,1,fp)==1){ /// fetch all record from file
if(strcmp(e.empid,empId) == 0){ ///if entered name matches with that in file
fndid:
printf("\n\nEnter new department: ");
scanf("%s",e.did);
rewind(fd);
while(fread(&d,depsize,1,fd)==1){ /// fetch all record from file
if(strcmp(e.did,d.depid) == 0){ ///if entered name matches with that in file
recfound = 1;
break;
}
}
if(recfound == 0)
{
printf("\nInvalid department. Please refer to department records\n");
rewind(fd); ///this moves file cursor to start of the file
printf("------------------------------------\n");
printf(" Id | Department Name\n");
printf("------------------------------------\n");
while(fread(&d,depsize,1,fd)==1){ /// read the file and fetch the record one record per fetch
printf("\n");
printf("%s\t| %-10s|",d.depid,d.dname); /// print depid and name
}
printf("\n\n");
goto fndid;
}
printf("\n\nEnter new first name: ");
scanf("%s",e.fname);
printf("\nEnter new last name: ");
scanf("%s",e.lname);
printf("\nEnter new age: ");
scanf("%d",&e.age);
printf("\nEnter new basic salary: ");
scanf("%f",&e.bs);
fseek(fp,-recsize,SEEK_CUR); /// move the cursor 1 step back from current position
fwrite(&e,recsize,1,fp); /// override the record
break;
}
}
printf("\nModify another record(y/n)");
fflush(stdin);
another = getchar();
}
break;
case '4':
system("clear");
rewind(fp); ///this moves file cursor to start of the file
printf("----------------------------------------------------------------------\n");
printf(" Id | DepId | First Name | Last Name | Age |Basic\n");
printf("----------------------------------------------------------------------\n");
while(fread(&e,recsize,1,fp)==1){ /// read the file and fetch the record one record per fetch
printf("\n");
printf(" %s\t| %s\t| %-15s| %s\t\t| %d\t\t| %.2f",e.empid,e.did,e.fname,e.lname,e.age,e.bs); /// print the name, age and basic salary
}
another = 'y';
while(another == 'y'){
printf("\n\nEnter id of employee to delete: ");
scanf("%s",empId);
ft = fopen("Temp.csv","wb"); /// create a intermediate file for temporary storage
rewind(fp); /// move record to starting of file
while(fread(&e,recsize,1,fp) == 1){ /// read all records from file
if(strcmp(e.empid,empId) != 0){ /// if the entered record match
fwrite(&e,recsize,1,ft); /// move all records except the one that is to be deleted to temp file
}
}
fclose(fp);
fclose(ft);
remove("EMP.csv"); /// remove the orginal file
rename("Temp.csv","EMP.csv"); /// rename the temp file to original file name
fp = fopen("EMP.csv", "rb+");
printf("Delete another record(y/n)");
fflush(stdin);
another = getchar();
}
break;
case '5':
//fclose(fp); /// close the file
goto mmenu; /// go to main menu
}
}
case '3':
fclose(fp); /// close the file
fclose(fd);
exit(0); /// exit from the program
}
}
return 0;
}