|
我是想要删除在binary file的一些资料,其他没有被选取delete的资料还会出现在本来的地方,请各位大大帮帮忙,拜托拜托<3
void deleterecord()
{
char recno[20];
char ch;
struct customerdetails t;
FILE *fp, *ft;
fp=fopen("customer.dat", "rb");
ft=fopen("Temp.dat", "wb");
printf("Enter the customer Number to delete : ");
scanf("%s", recno);
while((fread(&t, sizeof(t), 1, fp)))
{
if(strcmp(recno,t.id)==0)
{
printf("\ncustomer Number : %s", t.id);
printf("\ncustomer Name : %s",t.name1);
printf("\ncustomer contact : %s",t.contact);
printf("\nBook ID:%s",t.bookid);
printf("\nBook Name:%s",t.bookname);
printf("\nBook Type:%s",t.booktype);
printf("\nAre you sure want to delete?(Y/N)");
fflush(stdin);
scanf("%c", &ch);
}
}
if(ch=='y'||ch=='Y')
{
rewind(fp);
printf("Delete Successful");
while((fread(&t, sizeof(t), 1, fp)))
{
if(strcmp(recno,t.id)!=0)
{
fwrite(&t, sizeof(t), 1, ft);
}
}
}
else
fclose(fp);
fclose(ft);
remove("customer.dat");
rename("Temp.dat", "customer.dat");
}
|
|