I am creating this change function. void cha(struct cont x);
, It will ask for the lname then check if its in the file. After that, It will edit.Asks for the lname and fname again. It works but it writes at the bottom of the file.
struct cont
{
char lname[20];
char fname[20];
}s;
void cha(struct cont x)
{
FILE *fp;
char lname[20];
int flag=0;
fp=fopen("database.dat","a+");
if(fp==NULL)
{
printf("file error");
}
else
{
printf("\nenter lname: ");
gets(lname);
while(fscanf(fp,"%s %s",x.lname,x.fname)==2)
{
if(strcmp(lname,x.lname)==0)
{
printf("enter lname: ");
gets(x.lname);
printf("enter fname: ");
gets(x.fname);
fseek(fp,-sizeof(x),SEEK_CUR);
fprintf(fp,"%s %s\n",x.lname,x.fname);
flag=1;
break;
}
}
if(flag==1)
{
printf("success!");
}
else
{
printf("data not found.");
}
}
fclose(fp);
}