-
Notifications
You must be signed in to change notification settings - Fork 0
/
source_code.c
407 lines (364 loc) · 12.5 KB
/
source_code.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
404
405
406
407
// EMPLOYEE DATABASE MANAGEMENT SYSTEM
#include <stdio.h> //for input output functions like printf, scanf
#include <stdlib.h>
#include <conio.h>
#include <windows.h> //for windows related functions (not important)
#include <string.h> //string operations
//#include<graphics.h>
/* List of Global Variable */
COORD coord = {0,0}; // top-left corner of window
/*
function : gotoxy
@param input: x and y coordinates
@param output: moves the cursor in specified position of console
*/
void gotoxy(int x,int y)
{
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);
}
void add(void);
void display(void);
void modify(void);
void deletee(void);
void menu(void);
int admin()
{
system("COLOR 4e");
printf("\t====================================================================================================\n");
printf("\t||| |||\n");
printf("\t||| |||\n");
printf("\t||| |||\n");
printf("\t||| |||\n");
printf("\t||| ********************************************** |||\n");
printf("\t||| |||\n");
printf("\t||| EMPLOYEE DATATBASE MANAGEMENT SYSTEM |||\n");
printf("\t||| |||\n");
printf("\t||| ********************************************** |||\n");
printf("\t||| |||\n");
printf("\t||| |||\n");
printf("\t||| |||\n");
printf("\t||| |||\n");
printf("\t====================================================================================================\n");
printf("\n\n\n\t\t\t\t1.ADMIN\n\n\t\t\t\t2.EXIT\n\n\n\n");
printf("\t\t\tEnter your choice : ");
char ch2=0;
ch2=fgetc(stdin);
if (ch2==49)
{
gotoxy(0,0);
system("cls");
return 0;
}
else if(ch2==50)
{
printf("\n\n\nEXITING.........");
printf("\nPress any key to continue...\n");
getch();
exit(1);
}
else
{
gotoxy(0,0);
system("cls");
main();
return 1;
}
}
int login()
{
int a=0,i=0;
char uname[10],c=' ';
char pword[10],code[10];
char user[10]="user";
char pass[10]="pass";
printf("\n\nComplete the password setup to enter the management system\n");
do
{
printf("\n\n\t\t :::::::::::::::::::::::::: LOGIN FORM :::::::::::::::::::::::::: \n");
printf(" \n\t\t ENTER USERNAME:- ");
scanf("%s", &uname);
printf(" \n\t\t ENTER PASSWORD:- ");
while(i<10)
{
pword[i]=getch();
c=pword[i];
if(c==13) break;
else printf("*");
i++;
}
pword[i]='\0';
i=0;
if(strcmp(uname,"user")==0 && strcmp(pword,"pass")==0)
{
printf("\n ACCESS GRANTED... \n");
printf(" \n\n WELCOME TO EMPLOYEE RECORD MANAGEMENT SYSTEM !!!! LOGIN IS SUCCESSFUL");
printf("\n\n\n LOADING PLEASE WAIT...");
for(i=0; i<5; i++)
{
printf(".");
Sleep(500);
}
printf("\n\n\n\t\t\t\tPress any key to continue...");
getch();
return 0;
break;
}
else
{
printf("\n\nSORRY !!!! LOGIN IS UNSUCESSFUL");
a++;
getch(); //holds the screen
}
}while(a<=2);
if (a>2)
{
printf("\n\nSorry you have entered the wrong username and password for three times!!!");
printf("\nPress any key to continue...");
getch();
return 1;
}
system("cls");
}
char ch2=0;
FILE *fp, *ft; // file pointers
struct emp e; // structure variable creation
long int recsize; // size of each record of employee
char empname[40],empid[10]; // string to store name of the employee
struct emp // structure that represent a employee
{
char name[40],id[10]; // name of employee
int age; // age of employee
char address[20]; // address of employee
float bs; // basic salary of employee
};
void menu() // Main function started
{
char choice;
int i=0;
while(1)
{
system("cls"); // clear the console window
printf(" \n :::::::::::::::::::::::::: |EMPLOYEES RECORD MANAGEMENT| :::::::::::::::::::::::::: \n");
gotoxy(30,05); // move the cursor to postion 30, 10 from top-left corner
printf("1> Add Employee's Records"); // option for add record
gotoxy(30,07);
printf("2> List Employee's Records"); // option for showing existing record
gotoxy(30,9);
printf("3> Modify Employee's Records"); // option for editing record
gotoxy(30,11);
printf("4> Delete Employee's Records"); // option for deleting record
gotoxy(30,13);
printf("5> Return to the main menu"); // exit from the program
gotoxy(30,15);
printf("Your Choice: "); // enter the choice 1, 2, 3, 4, 5
fflush(stdin); // flush the input buffer
choice = getche(); // get the input from keyboard
switch(choice)
{
case '1': // if user press 1 then add funtion is called
add();
break;
case '2': // if user press 2 then display funtion is called
display();
break;
case '3': // if user press 3 then modify funtion is called
modify();
break;
case '4': // if user press 4 then delete funtion is called
deletee();
break;
case '5':
printf("\n\n\n RETURNING TO MAIN MENU...");
for(i=0; i<5; i++)
{
printf(".");
Sleep(500);
}
printf("\n\n\n\t\t\t\tPress any key to continue...");
getch();
fclose(fp); // close the file
system("cls");
admin();
}
}
}
int main()
{
int i=0;
if (admin()==0)
{
/*
open the file in binary read and write mode
if the file EMP.DAT already exists then it open that file in read write mode
if the file doesn't exit it simply create a new copy
*/
if (login()==0)
{
fp = fopen("EMP.DAT","rb+");
if(fp == NULL)
{
fp = fopen("EMP.DAT","wb+");
if(fp == NULL)
{
printf("Connot open file");
exit(1);
}
}
recsize = sizeof(e); // sizeo of each record i.e. size of structure variable e
menu();
}
else
{
gotoxy(0,0);
system("cls");
admin(); // Calling the menu function
}
}
}
void add() // This function is used to add the details of the employee
{
system("cls");
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
char another = 'y';
while(another == 'y') // if user want to add another record
{
system("cls");
printf("\n\n\t\t\t\t\t*****************************************");
printf("\n");
printf("\n\t\t\t\t\t\tADDING EMPOLYEE'S DETAILS");
printf("\n");
printf("\n\t\t\t\t\t*****************************************");
printf("\n\n\nEnter unique id: ");
scanf("%s",e.id);
printf("\nEnter name: ");
scanf("%s",e.name);
printf("\nEnter age: ");
scanf("%d", &e.age);
printf("\nEnter Address:");
scanf("%s",e.address);
printf("\nEnter basic salary: ");
scanf("%f", &e.bs);
fwrite(&e,recsize,1,fp); // write the record in the file
printf("\n\n\nRecord added successfully\n");
printf("Add another record?? (y/n) ");
printf("\nEnter your input: ");
fflush(stdin);
another = getche();
printf("\n\n\n\t\t\t\tPress any key to continue...");
getch();
}
}
void display() // This function is used to display all the details of the employee
{
system("cls");
printf("\n\n\t\t\t\t\t********************************************");
printf("\n");
printf("\n\t\t\t\t\t\tDISPLAYING EMPOLYEE'S DETAILS");
printf("\n");
printf("\n\t\t\t\t\t********************************************");
rewind(fp); // this moves file cursor to start of the file
while(fread(&e,recsize,1,fp)==1) // read the file and fetch the record one record per fetch
{
printf("\n\n\n\nID : %s",e.id);
printf("\nNAME : %s",e.name);
printf("\nAGE : %d",e.age);
printf("\nADDRESS : %s",e.address);
printf("\nBASIC SALARY: %.2f",e.bs);
}
printf("\n\n\n\t\t\t\tPress any key to continue...");
getch();
}
void modify() // This function is used to modify the details of the employee
{
system("cls");
printf("\n\n\t\t\t\t\t********************************************");
printf("\n");
printf("\n\t\t\t\t\t\tMODIFYING EMPOLYEE'S DETAILS");
printf("\n");
printf("\n\t\t\t\t\t********************************************");
char another = 'y';
while(another == 'y')
{
printf("\n\nEnter the employee id to modify: ");
scanf("%s", empid);
rewind(fp);
int flag=0;
while(fread(&e,recsize,1,fp)==1) // fetch all record from file
{
if(strcmp(e.id,empid) == 0) // if entered name matches with that in file
{
flag=1;
printf("\n\nRECORD FOUND\n");
printf("\nEnter new name,age,address and basic salary: \n\n");
scanf("%s%d%s%f",e.name,&e.age,&e.address,&e.bs);
fseek(fp,-recsize,SEEK_CUR); // move the cursor 1 step back from current position
fwrite(&e,recsize,1,fp);
printf("\n\nRECORD MODIFIED SUCCESSFULLY\n"); // override the record
break;
}
}
if(flag==0)
{
printf("\n\nRECORD NOT FOUND");
}
printf("\n\nModify another record?? (y/n) ");
printf("\nEnter your input: ");
fflush(stdin);
another = getche();
printf("\n\n\n\t\t\t\tPress any key to continue...");
getch();
}
}
void deletee() //This function is used to delete the details of the employee
{
system("cls");
printf("\n\n\t\t\t\t\t********************************************");
printf("\n");
printf("\n\t\t\t\t\t\tDELETING EMPOLYEE'S DETAILS");
printf("\n");
printf("\n\t\t\t\t\t********************************************");
char another = 'y';
int flag=0;
while(another == 'y')
{
system("cls");
printf("\n\nEnter id of employee to delete: ");
scanf("%s",empid);
ft = fopen("Temp.dat","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.id,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
}
else
{
flag=1;
}
}
fclose(fp);
fclose(ft);
remove("EMP.DAT"); // remove the orginal file
rename("Temp.dat","EMP.DAT"); // rename the temp file to original file name
fp = fopen("EMP.DAT", "rb+");
if(flag==1)
{
printf("\n\nRECORD FOUND");
printf("\nRECORD DELETED SUCCESSFULLY\n");
}
else
{
printf("\n\nRECORD NOT FOUND\n");
}
printf("\n\nDelete another record?? (y/n) ");
printf("\nEnter your input: ");
fflush(stdin);
another = getche();
printf("\n\n\n\t\t\t\tPress any key to continue...");
getch();
}
}