First of all, I'm sorry, but your code is really "ugly"!
The variables
tempx could have a describing name so it will be easier to follow the flow.
Doesn't make much sense to define a structure than copy only some of its fields in the file.
Instead of:
struct emprec
{
int empid;
int operation;
char name[20];
};
if (temp2.operation==1) {
count++;
fwrite(&temp2.empid,sizeof(temp2.empid),1,fd);
fwrite(&temp2.name,20,1,fd);
fclose(fd);
}
Define a record structure and perform I/O on the whole structure, that is faster and less error prone:
struct Record
{
int empid;
char name[20];
};
struct emprec
{
int operation;
struct Record record;
};
if (temp2.operation==1) {
count++;
fwrite(&temp2.record,sizeof(struct Record),1,fd);
fclose(fd);
}
In this way you would have avoided the error you made in the deleting routine:
...
while(i<=count)
{
fread(&temp7.empid,sizeof(temp7.empid),1,fd); fread(&temp7.name,sizeof(temp7.name),1,fd);
if(temp2.empid==temp7.empid)
{
printf("A requested by name found and deleted\n");
found = 1;
count--;
}
else
{
fwrite(&temp7,sizeof(temp7),2,fd1); }
...
Could have been:
...
while(i<=count)
{
fread(&temp7.record,sizeof(struct Record),1,fd);
if(temp2.record.empid==temp7.record.empid)
{
printf("A requested by name found and deleted\n");
found = 1;
count--;
}
else
{
fwrite(&temp7.record,sizeof(struct Record),1,fd1);
}
...