I'm trying to delete a record from a file, by copying all of the records to a temporary file, except the one to be deleted. Then I delete the old file and rename the temporary file. Before renaming, I'm testing to see if the data actually transfers, but when I enter the prompted date and details, the program just freezes, even though the data I enter is from a record in the file. Can anyone help?
Here's the code:
void del_income() {
Income I;
Delete D;
FILE *INCOME = fopen("C:\\income.txt", "a");
FILE *TEMP = fopen("C:\\temporary.txt", "a");
printf("Enter date of record to be deleted:");
scanf("%s", D.date);
printf("Enter job details of records to be deleted:");
scanf("%s", D.details);
while (1) {
fscanf(INCOME, "%s %s %s %d %d %d %d %d",
I.date, I.details, I.vehicleno, &I.rate, &I.hours, &I.subtotal,
&I.vat, &I.total);
if (feof(INCOME))
break;
if (strcmp(I.date, D.date) == 1 && strcmp(I.details, D.details) == 1)
fprintf(TEMP, "%s %s %s %d %d %d %d %d",
I.date, I.details, I.vehicleno, I.rate, I.hours, I.subtotal,
I.vat, I.total);
}
fclose(INCOME);
fclose(TEMP);
system("cls");
}
IncomeandDelete, a complete program including all#includedirectives and amainfunction, contents ofincome.txt, and sample input.feof(INCOME). Instead, test the return value offscanf. Do not rely onstrcmpto return 1. The only guarantee from the C standard is that it return a value greater than 0 to indicate “greater than”.fopen().