system("cls");
another = 'y';
while(another == 'y')
{
printf("\nEnter 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)");
another = getche();
}
这是使用员工姓名从 c 中的二进制文件中删除条目的代码!但是当我想通过 emp id 删除它时,它不会删除条目,而是删除文件的所有内容!
case '4':
system("cls");
another = 'y';
while(another == 'y')
{
printf("\nEnter employee id to delete ");
scanf("%d",&empid);
ft = fopen("Temp.dat","wb");
rewind(fp);
while(fread(&e,recsize,1,fp) == 1)
{
if(e.userid!=empid)
{
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)");
another = getche();
}