#include "stdio.h"
main()
{
FILE*fp,*ft;
char another,choice;
struct emp
{
char name [20];
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+");
exit();
}
recsize=sizeof(e);
while(1)
{
clrscr();
printf("\n 1.Add records");
printf("\n 2.List records");
printf("\n 3.Modifay records");
printf("\n 4.Delete records");
printf("\n 0.Exit");
printf("\n Enter your choice");
fflush (stdin);
choice=getche();
switch(choice)
{
case '1':
fseek(fp,0,SEEK_END);
another='y';
while(another=='y')
{
printf("\n enter name age and basic salary");
scanf("%s %d %f",&e.name,&e.age,&e.bs);
fwrite(&e,recsize,1,fp);
printf("\n add another records(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);
getch();
break;
case '3':
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 and 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':
another ='y';
while(another=='y')
{
printf("\n enter name of employee to 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("EMP.DAT");
rename("TEMP.DAT","EMP.DAT");
fp=fopen("EMP.DAT","rb+");
printf("delete another record(y/n)");
fflush(stdin);
another=getche();
}
break;
case '0':
fclose(fp);
exit();
}
}
}
Related Links :