I'm trying to create a small book library in C so I wrote a function that insert a book by it's name, id and quantity. For some reason the program is ruining fine and the function seems to be working but the external file remain unchanged (no new data is added). I checked the path of the file but still the problem persist. how can I fix this?
here's the function and the structure:
struct library{
int id;
int qty;
char name[50];
};
void InsertBook()
{
struct library b;
FILE *books;
if((books=fopen("C:\\mybooks.txt","a+")==NULL))
{
printf("file not found\n");
}
else
{
printf("You will need to enter a name, ID, and quantity of the book.\n");
printf("please enter book name:");
fflush(stdin);
fgets(b.name,SIZE,stdin);
fputs(b.name,books);
printf("please enter book ID:");
scanf("%d",&b.id);
printf("please enter book quantity:");
scanf("%d",&b.qty);
fprintf(books,"%d %s %d\n",b.name,b.id,b.qty);
fclose(books);
}
}```