-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile in binary.cpp
More file actions
175 lines (173 loc) · 3.87 KB
/
file in binary.cpp
File metadata and controls
175 lines (173 loc) · 3.87 KB
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
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define HEADSIZE sizeof(int)
typedef struct
{
int deleted;
char name[100];
int age;
float sal;
int id;
}emp;
void inputdata(emp *ep)
{
printf("enter employee's name:");
fflush(stdin);
gets(ep->name);
printf("enter employee's age:");
scanf("%d",&ep->age);
printf("enter employee's salary:");
scanf("%f",&ep->sal);
printf("enter employee's id:");
scanf("%d",&ep->id);
ep->deleted=0;
}
void printdata(emp *e)
{
printf(" employee's name is %s\nage is %d\nsalary is %7.2f\nid is %d\n",e->name,e->age,e->sal,e->id);
}
/*int searchbyname(FILE *fp,char *name)
{
emp e;
while(!feof(fp))
{
fread(&e,sizeof(emp),1,fp);
if(!e.deleted && strcmpi(e.name,name))
{
fseek(fp,-sizeof(emp),SEEK_CUR);
return 1;
}
}
return 0;
}*/
int main()
{
int ch,i,nrec=0;
char name[50];
emp e;
const char *a="c:\\users\\lenovo\\desktop\\riddhi.txt";
FILE *fp;
fp=fopen(a,"r+b"); //if file exists open in read-write mode.(read is piority)
if(fp==NULL) //file does not exist.write-read mode.
{
fclose(fp);
fp=fopen(a,"w+b");
}
else
{
fseek(fp,0,SEEK_SET);
fread(&nrec,sizeof(int),1,fp);
printf("no of records=%d\n",nrec);
}
while(1)
{
printf("1.Add\n2.Display all\n3.Search by id\n4.Dispaly ith record\n5.Search by name\n6.Edit\n7.Delete\n8.Exit\n");
printf("enter choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: inputdata(&e); //Add record.
fseek(fp,0,SEEK_SET);
nrec++;
printf("from add,nrec=%d\n",nrec);
fwrite(&nrec,sizeof(int),1,fp);
fseek(fp,HEADSIZE+(nrec-1)*sizeof(emp),SEEK_SET);
fwrite(&e,sizeof(emp),1,fp);
break;
case 2: fseek(fp,HEADSIZE,SEEK_SET); //Display All.
printf("display all,nrec=%d\n",nrec);
for(i=1;i<=nrec;i++)
{
fread(&e,sizeof(emp),1,fp);
if(!e.deleted)
printdata(&e);
}
break;
case 3: int id; //Search by id.
printf("enter id you want to search:");
scanf("%d",&id);
fseek(fp,HEADSIZE,SEEK_SET);
for(i=1;i<=nrec;i++)
{
fread(&e,sizeof(emp),1,fp);
if(!e.deleted && e.id==id)
{
printdata(&e);
break;
}
}
if(i>nrec)
printf("Record not found");
break;
case 4: int p; //Display ith record.
printf("enter record no you want to display:");
scanf("%d",&p);
if(p<=nrec)
{
fseek(fp,HEADSIZE+(p-1)*sizeof(emp),SEEK_SET);
fread(&e,sizeof(emp),1,fp);
printdata(&e);
}
else
printf("Record not found");
break;
case 5: //Search by name.
printf("enter the name you want to search:");
gets(name);
fseek(fp,HEADSIZE,SEEK_SET);
for(i=1;i<=nrec;i++)
{
fread(&e,sizeof(emp),1,fp);
if(!e.deleted && strcmpi(e.name,name)==0)
{
printdata(&e);
break;
}
}
if(i>nrec)
printf("Record not found");
break;
case 6: //Edit by name.
printf("enter the name you want to edit:");
fflush(stdin);
gets(name);
fseek(fp,HEADSIZE,SEEK_SET);
for(i=1;i<=nrec;i++)
{
fread(&e,sizeof(emp),1,fp);
if(!e.deleted && strcmpi(e.name,name)==0)
{
fseek(fp,-sizeof(emp),SEEK_CUR);
inputdata(&e);
fwrite(&e,sizeof(emp),1,fp);
break;
}
}
if(i>nrec)
printf("Record not found");
break;
case 7: //Delete by name.
printf("enter the name you want to delete:");
fflush(stdin);
gets(name);
fseek(fp,HEADSIZE,SEEK_SET);
for(i=1;i<=nrec;i++)
{
fread(&e,sizeof(emp),1,fp);
if(!e.deleted && strcmpi(e.name,name)==0)
{
e.deleted=1;
fseek(fp,-sizeof(emp),SEEK_CUR);
fwrite(&e,sizeof(emp),1,fp);
printf("%dth record deleted\n",i);
break;
}
}
if(i>nrec)
printf("Record not found");
break;
case 8: exit(1);
}
}
}