I have a program that is supposed to delete records from a file, I tried opening the file to read and write and using fgets to input each line. I'd then sscanf the input to a string and test to see if one of the items in the line is the record. If it is then I use continue to skip it and if it isn't then I fputs the line back into the file.
Supposedly this should leave the file as it was except without the one line I wanted to delete, but it's not working at all. Here's the code
Code:
int delete_rec()
{
FILE*fpt;
unsigned long number;
char buffer[500],
pInput[500];
struct cust TEMP;
printf("\nDELETE RECORD");
if(!(fpt=fopen("cust.dat","rw"))){
printf("Error opening cust.dat for delete");
return (1);
}
printf("\nCustomer Number:");
scanf("%d", &number);
fflush(stdin);
while(fgets(buffer, sizeof(buffer), fpt) != NULL){
sscanf(pInput, "%d%*c%49[^;]%*c%49[^;]%*[^OoSs]%c", &TEMP.no, TEMP.name,
TEMP.add, &TEMP.status);
if(TEMP.no == number)
continue;
fputs(buffer, fpt);
}
if(fclose(fpt) == EOF){
printf("Error closing file\a\n");
return 1;
}
printf("\n\nDeleted. Press ENTER to continue.\n");
getchar();
return 0;
}