#include<stdlib.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
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;
clrscr();
fp=fopen("EMP.DAT","rb+");
if ( fp== NULL)
{
fp= fopen("EMP.DAT ","wb+");
if (fp==NULL)
{
puts("\nCannot open file");
exit(1);
}
}
recsize= sizeof(e);
while(1)
{
gotoxy(10,10);
printf("1. Add Records");
gotoxy(10,12);
printf("2. Display Records");
gotoxy(10,14);
printf("3. Modify Records");
gotoxy(10,16);
printf("4. Delete Records");
gotoxy(10,18);
printf(" 0. Exit");
gotoxy(10,20);
printf("Your choice");
fflush(stdin);
choice=getche();
switch(choice)
{
case'1':
clrscr();
fseek(fp,0,SEEK_END);
another='Y';
while(another=='Y')
{
printf("\n Enter name, age and basic sal");
scanf("%s%d%f",e.name,&e.age,&e.bs);
fwrite(&e,recsize,1,fp);
printf("\n Add another record(Y/N)");
fflush(stdin);
another= getche();
}
break;
case'2':
clrscr();
rewind(fp);
while( fread(&e,recsize,1,fp)==1)
printf("\n%s %d %f",e.name,e.age,e.bs);
break;
case'3':
clrscr();
another='Y';
while(another=='Y')
{
printf("\n Enter name of employee to modify");
scanf("%s",empname);
rewind(fp);
while(fread(&e,recsize,1,fp)==1)
{
if (strcmp(e.name,empname)==0)
{
printf("\n Enter new name , age & bs");
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':
clrscr();
another='Y';
while( another=='Y')
{
printf("\n Enter name of employeeto delete");
scanf("%s", empname);
ft=fopen("TEMP.DAT","wb");
rewind(fp);
while(fread(&e,recsize,1,fp)==1)
{
if (strcmp(e.name,empname)!=0)
fwrite(&e,recsize,1,ft);
}
fclose(fp);
fclose(ft);
remove("TEMP.DAT");
rename("TEMP.DAT","EMP.DAT");
fp=fopen("EMP.DAT","rb+");
printf("\nDo you want to Delete another record(Y/N)");
fflush(stdin);
another=getche();
}
break;
case'0':
fclose(fp);
exit(1);
}
}
}