I've made a program that reads a file named "books.txt" and displays its contents. In the file, there is a price for each of the 4 books.
I'm not sure how to make my program increase the price of the book (percent) depending on user inputs; for example, the user is prompted to enter a number and that number (which is a percentage) is multiplied by the price of each book and gives the updated price.
How do I do it?
Here's my code:
int main() {
/* File pointer to hold reference to our file */
FILE * fPtr;
char buffer[BUFFER_SIZE];
int totalRead = 0;
/*
*/
fPtr = fopen("books.txt", "r");
if (fPtr == NULL) {
printf("Unable to open file.\n");
printf("Please check whether file exists and you have read privilege.\n");
exit(EXIT_FAILURE);
}
printf("File opened successfully. Reading file contents line by line. \n\n");
while (fgets(buffer, BUFFER_SIZE, fPtr) != NULL) {
totalRead = strlen(buffer);
/*
*/
buffer[totalRead - 1] = buffer[totalRead - 1] == '\n'
? '\0'
: buffer[totalRead - 1];
printf("%s\n", buffer);
}
fclose(fPtr);
return 0;
}