#include<stdio.h>
#include<stdlib.h>
#include<conio.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)
{
clrscr();
gotoxy(30,10);
printf("1.Add records");
gotoxy(30,12);
printf("2.List rcords");
gotoxy(30,12);
printf("0.Exit");
gotoxy(30,20);
printf("Your choice");
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 '0':
fclose(fp);
exit(0);
}
}
}
Please it's not compiling showing some errors help me to execute. And also try to explain me line to line what will happen in this program.
And my final doubt is, in this program i about to create a new record. I want to allocate a code (automatically) for every member who had newly added a record with that they will be able to modify their record. Is it possible if yes, explain me with the code.