-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMAHAFILE.c
131 lines (114 loc) · 2.42 KB
/
MAHAFILE.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
// A menu driven program for elementry database management
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void main()
{
FILE *fp, *ft;
char another, choice;
struct emp
{
char name[40];
int age;
float bs;
};
struct emp e;
char empname[40];
long int recsize;
fp = fopen("EMP.DAT", "rb+");
if (fp == NULL)
{
fp = fopen("EMP.DAT", "wb+");
if (fp == NULL)
{
puts("Cannot Open File....");
exit(1);
}
}
recsize = sizeof(e);
while (1)
{
gotoxy(30, 10);
printf("1.Add Records");
gotoxy(30, 12);
printf("2List Records");
gotoxy(30, 14);
printf("3.Modify Records");
gotoxy(31, 16);
printf("4.Delete Records");
gotoxy(30, 20);
printf("Your choice:\n");
fflush(stdin);
choice = getche();
switch (choice)
{
case 1:
fseek(fp, 0, SEEK_END);
another = 'y';
while (another == 'y')
{
printf("\nEnter name,age and basic salary:");
scanf("%s %d %f", e.name, &e.age, &e.bs);
fwrite(&e, recsize, 1, fp);
printf("\nAdd another Record (y/n):");
fflush(stdin);
another = getche();
}
break;
case 2:
rewind(fp);
while (fread(&e, recsize, 1, fp) == 1)
printf("\n%s %d %f", e.name, e.age, e.bs);
break;
case 3:
another = 'y';
while (another == 'y')
{
printf("\nEnter name of employee to modify");
scanf("%s", empname);
rewind(fp);
while (fread(&e, recsize, 1, fp) == 1)
{
if (strcmp(e.name, empname) == 0)
{
printf("\nEnter new name, age and bs:\n");
scanf("%s %d %f", e.name, &e.age, &e.bs);
fseek(fp, -recsize, SEEK_CUR);
fwrite(&e, recsize, 1, fp);
break;
}
}
printf("\nModify another record(y/n)");
fflush(stdin);
another = getche();
}
break;
case 4:
another = 'y';
while (another == 'y')
{
printf("Enter name of employee to delete:");
scanf("%s", empname);
ft = fopen("TEMP.DAT", "wb");
rewind(fp);
while (fread(&e, recsize, 1, ft) == 1)
{
if (strcmp(e.name, empname) != 0)
fwrite(&e, recsize, 1, ft);
}
fclose(fp);
fclose(ft);
remove("EMP.DAT");
rename("TEMP.DAT", "EMP.DAT");
fp = fopen("EMP.DAT", "rb+");
printf("\nDelete another record(y/n):");
fflush(stdin);
another = getche();
}
break;
case 0:
fclose(fp);
exit(0);
}
}
}