When I go to delete something by using my delete function , i end up wiping the entire .DAT file. need help, can anyone tell me what im doing wrong
Code:
void delete_student()
{
another='Y';
while(another=='Y'|| another=='y')
{
printf("\nEnter the id of the student to be deleted : \n");
scanf("%s",&stud.id,student);
buffer = (char *)malloc(1000*sizeof(char));
memset(buffer,0,1000*sizeof(char));
ptr = buffer;
ft=fopen("TEMP.DAT","wb");
if(ft !=NULL)
{
while(!feof(fp))
{
fgets(stud.id,9,fp);
if(strcmp(stud.id,"%s"),student !=0)
{
strcpy(ptr,stud.id);
ptr += strlen(stud.id);
}
fwrite(&stud,recsize,1,fp);
fclose(fp);
fclose(ft);
remove("STUD.DAT");
rename("TEMP.DAT","STUD.DAT");
fp=fopen("STUD.DAT","a+b");
printf("Delete another Record(Y/N): \n");
fflush(stdin);
another=getchar();
ft=fopen("TEMP.DAT","w");
fprintf(fp,"%s",buffer);
}
system("pause");
system("cls");
menu_function();
}
}
thats the first function I tried using a buffer.
Code:
void delete_student()
{
another='Y';
while(another=='Y'|| another=='y')
{
printf("\nEnter the id of the employee to be deleted : \n");
scanf("%s",&stud.id);
ft=fopen("TEMP.DAT","wb");
rewind(fp);
while(fread(&stud,recsize,1,fp)==1)
{
if(strcmp(stud.id,id)!=0)
fwrite(&stud,recsize,1,ft);
}
fclose(fp);
fclose(ft);
remove("STUD.DAT");
rename("TEMP.DAT","STUD.DAT");
fp=fopen("STUD.DAT","rb+");
printf("Delete another Record(Y/N): \n");
fflush(stdin);
another=getchar();
}
system("pause");
system("cls");
menu_function();
}
heres the other function