I am writing a program which will update the price of information which is coming from a text file and then display to stdout.
The issue I am having is that I will try to read information from the existing text file with information into the structure. Then edit the price variable.
I am unable to write, append, or do any of the + feature of this file I can only read it.
The change in price will display to standard output fine but it won't change the price value in the text file existing. I attached the structure and the function I'll be using to change the variable.
void printPrice() {
double chg;
puts("=====================================");
printf("please enter a change of a value using precentage ");
scanf("%lf", & chg);
printf("Prices changed\n");
printf("writing to book file completed\n");
puts("=====================================\n");
FILE * file = fopen("books.txt", "w");
if (file == NULL) {
printf("error");
exit(1);
}
while (!feof(file)) {
double chng = (chg / 100);
double pric = atof(mv.price);
pric = pric + pric * chng;
puts("--------------------------");
fgets(mv.ID, 6, file);
printf("ID:%s", mv.ID);
fgets(mv.ttle, 50, file);
printf("TITLE: %s", mv.ttle);
fgets(mv.author, 80, file);
printf("AUTHOR: %s", mv.author);
fgets(mv.year, 7, file);
printf("YEAR: %s", mv.year);
fgets(mv.price, 7, file);
sprintf(mv.price, "%.2f\n", pric);
printf("PRICE: %s", mv.price);
puts("--------------------------");
fprintf(file, "%s%s%s%s%s", mv.ID, mv.ttle, mv.author, mv.year, mv.price);
fflush(file);
}
fclose(file);
}