Hi, I'm making a library management program that takes in a book's characteristics (ISBN, author, year published, etc.), stores it in an array of structs and stores this into a file.
void addBook(struct book b) { bookArray[a++] = b; }
int main() { bookArray = malloc(sizeof(struct book)); struct book b = {"0-394-49219-6", "Tinker, Sailor, Soldier, Spy", 1, 1974, "spy fiction", "Le Carre", 0}; bookArray[0] = b; addBook(b); write(); readFile();
for(int i = 0; i < a; i++) { printf("%s\t%s\t%d\t%d\t%s\t%s\t%d\n", bookArray[i].ISBN, bookArray[i].title, bookArray[i].accno, bookArray[i].year, bookArray[i].genre, bookArray[i].authname, bookArray[i].issued); } }
1. I'm not able to figure out how to store the value of ```a``` after the program exits. I thought of storing it in another file, but was wondering if there's a better way to do that.
2. How do I allocate dynamically increasing space (as the number of books increase) to ```bookArray```? I get only only one row printed from the loop if I run the program several times, despite ```db.txt``` storing all the runs.
I'm posting here for the first time and I hope I haven't violated any rules.