-
Notifications
You must be signed in to change notification settings - Fork 0
/
mini2.c
262 lines (251 loc) · 7.15 KB
/
mini2.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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int printmenu() {
int ch;
printf("[1].Add record of new employee\n");
printf("[2].Show records\n");
printf("[3].Search a record\n");
printf("[4].Delete a record\n");
printf("[5].Modify a record\n");
printf("[6].Show salary slip\n");
printf("[7].Exit\n");
printf("Enter ur choice :");
scanf("%d", &ch);
return ch;
}
int main(){
typedef struct dob {
int dd;
int mm;
int yy;
} date;
typedef struct employee {
char name[20];
int basic;
int pday;
int id;
date b, j;
}emp;
int choice, i, l, dd, mm, yy, id1, flag = 0;
int p, b;
long sal;
emp e;
char nxt, name[20], ch;
FILE *fp, *fn, *ft;
while(1) {
fp = fopen("new.txt", "rb+");
if(fp == NULL){
fp = fopen("new.txt","wb+");
if(fp == NULL){
printf("Cannot create file");
exit(1);
}
}
else{
fp = fopen("new.txt","ab+");
if(fp == NULL){
printf("Cannot OPEN file");
exit(1);
}
}
choice = printmenu();
switch(choice) {
case 1:
/***adding info of new employee***/
do {
printf("\nEnter emp name:");
fflush(stdin);
scanf("%s", e.name);
l = strlen(e.name);
e.name[l] = '\0';
printf("\nEnter date of birth of emp:");
scanf("%d%d%d", &e.b.dd, &e.b.mm, &e.b.yy);
printf("\nEnter emp basic salary:");
scanf("%d", &e.basic);
printf("\nEnter present days of emp:");
scanf("%d", &e.pday);
printf("\nEnter emp id:");
scanf("%d", &e.id);
printf("\nEnter date of joining:");
scanf("%d%d%d", &e.j.dd, &e.j.mm, &e.j.yy);
fwrite(&e, sizeof(e), 1, fp);
printf("\nDo u want to add another record?: y/n\n");
fflush(stdin);
scanf("%s", &ch);
} while(ch == 'y');
fclose(fp);
break;
case 2:
/***display***/
fseek(fp, 0, SEEK_SET); //this moves file cursor to start of the file
printf("-------------------------------------------------------------\n");
printf("ID\tName\t DOB\tbasic salary\tPresent Days\tJoining Date\n");
printf("-------------------------------------------------------------\n");
while(fread(&e, sizeof(e), 1, fp) == 1){ // read the file and fetch the record one record per fetch
printf("%d\t", e.id);
printf("%s\t", e.name);
printf("%d/%d/%d\t", e.b.dd, e.b.mm, e.b.yy);
printf("%d\t\t", e.basic);
printf("%d\t", e.pday);
printf("%d/%d/%d\n", e.j.dd, e.j.mm, e.j.yy);
}
break;
case 3:
/***search the record***/
printf("1. Search by name\n");
printf("2. Search by id\n");
printf("3.Search by joining date\n");
scanf("%d", &i);
switch(i) {
case 1:/***Search by name***/
printf("-------Search Employee By Name-------\n");
printf("Enter employee Name:\n");
scanf("%s",name);
rewind(fp);
while(fread(&e, sizeof(e), 1, fp)!=0) {
if(strcmp(e.name, name) == 0) {
printf("Record found:\n");
printf("ID:%d\tName:%s\tDOB:%d/%d/%d\tbasic salary:%d\tpresent days:%d\tJoin Date:%d/%d/%d\n", e.id, e.name, e.b.dd, e.b.mm, e.b.yy, e.basic, e.pday, e.j.dd, e.j.mm, e.j.yy);
flag = 1;
break;
}
}
fclose(fp);
if(flag == 0)
printf("Record not found\n");
break;
case 2:/***Search by id***/
printf("-------Search Employee By Id-------\n");
printf("Enter the emp id: ");
scanf("%d", &id1);
rewind(fp);
while(fread(&e, sizeof(e), 1, fp) == 1) {
if(e.id == id1) {
printf("Record found:\n");
printf("ID:%d\tName:%s\tDOB:%d/%d/%d\tbasic salary:%d\t\tpresent days:%d\tJoin Date:%d/%d/%d\n", e.id, e.name, e.b.dd, e.b.mm, e.b.yy, e.basic, e.pday, e.j.dd, e.j.mm, e.j.yy);
flag = 1;
break;
}
}
fclose(fp);
if(flag == 0)
printf("\nRecord not found");
break;
case 3:/***Search by joining date***/
printf("-------Search Employee By Id-------\n");
printf("Enter the date: ");
scanf("%d%d%d", &dd, &mm, &yy);
rewind(fp);
while(fread(&e, sizeof(e), 1, fp) == 1) {
if(dd == e.j.dd && mm == e.j.mm && yy == e.j.yy) {
printf("Record found:\n");
printf("ID:%d\tName:%s\tDOB:%d/%d/%d\tbasic salary:%d\tpresent days:%d\tJoin Date:%d/%d/%d\n", e.id, e.name, e.b.dd, e.b.mm, e.b.yy, e.basic, e.pday, e.j.dd, e.j.mm, e.j.yy);
flag = 1;
break;
}
}
fclose(fp);
if(flag == 0)
printf("\nRecord not found");
break;
}
break;
case 4:
/***delete***/
printf("Enter emp id to delete:");
scanf("%d", &id1);
ft = fopen("temp.txt", "ab+");
fn = fopen("abc.txt", "wb+");
while(fread(&e, sizeof(e), 1, fp) == 1) {
if(e.id == id1) {
fwrite(&e, sizeof(e), 1 , ft);
flag = 1;
}
else
fwrite(&e, sizeof(e), 1 , fn);
}
fclose(fp);
fclose(fn);
remove("new.txt");
rename("abc.txt", "new.txt");
if(flag == 0)
printf("Record not found\n");
else
printf("Record is deleted\n");
break;
case 5:
/***Modify the record***/
printf("Enter the employee name to modify:\n");
scanf("%s", name);
//rewind(fp);
while(fread(&e, sizeof(e), 1, fp) == 1) { /// fetch all record from file
if(strcmp(e.name, name) == 0) { ///if entered name matches with that in file
printf("Existing record:\n");
printf("ID:%d\tName:%s\tDOB:%d/%d/%d\tbasic salary:%d\tpresent days:%d\tJoin Date:%d/%d/%d\n", e.id, e.name, e.b.dd, e.b.mm, e.b.yy, e.basic, e.pday, e.j.dd, e.j.mm, e.j.yy);
printf("Update basic salary: y / n? :");
scanf("%s", &ch);
if(ch == 'y') {
printf("Enter new basic salary:");
scanf("%d", &e.basic);
}
printf("Update present days: y / n? :");
scanf("%s", &ch);
if(ch == 'y') {
printf("Enter present days:");
scanf("%d", &e.pday);
}
printf("Update name: y / n? :");
scanf("%s", &ch);
if(ch == 'y') {
printf("Enter modified name:");
scanf("%s", e.name);
}
printf("Update DOB: y / n? :");
scanf("%s", &ch);
if(ch == 'y') {
printf("Enter modified date:");
scanf("%d%d%d", &e.b.dd, &e.b.mm, &e.b.yy);
}
//printf("%u\n", &fp);
fseek(fp, -(sizeof(e)),SEEK_CUR); /// move the cursor 1 step back from current position
//printf("%u", &fp);
fwrite(&e, sizeof(e), 1, fp); /// overwrite the record
flag = 1;
break;
}
}
if(flag == 0)
printf("Record not found\n");
else{
printf("Record is Updated\n");
printf("ID:%d\tName:%s\tDOB:%d/%d/%d\tbasic salary:%d\tpresent days:%d\tJoin Date:%d/%d/%d\n", e.id, e.name, e.b.dd, e.b.mm, e.b.yy, e.basic, e.pday, e.j.dd, e.j.mm, e.j.yy);
}
fclose(fp);
break;
case 6:
/*calculate salary slip*/
printf("Enter ID of d emp: ");
scanf("%d", &id1);
while(fread(&e, sizeof(e), 1, fp) == 1) {
if(id1 == e.id) {
p = e.pday;
b = e.basic;
sal = p * b;
flag = 1;
}
}
if(flag == 1) {
printf("Salary Details:\n");
printf("ID:%d\tName:%s\tBasic:%d\tPresent Days:%d\tNet Salary:%lu\n", e.id, e.name, e.basic, e.pday, sal);
}
fclose(fp);
break;
case 7:
/*Exit*/
exit(0);
break;
}
}
return 0;
}