The shelf stores one book?
Code:
ifp = fopen("library.txt", "r");
You should be checking to see if your file failed to open or not before you try to use it.
Code:
fscanf(ifp, "%d", &cases);
You should probably be reading entire lines at a time into a buffer with something like fgets instead, simply because it's cleaner to do so.
Code:
fscanf(ifp, "%d", &instruct);
You should really be checking your return values to see if you get what you expect or not.
You might consider using a switch:
Code:
if (instruct == 1) {
addBook(ifp, &myLib, shelf);
} else if (instruct == 2) {
removeBook(ifp, &myLib, &shelf);
} else if (instruct == 3) {
int k = 0;
fscanf(ifp, "%s", &shelf.title[k]);
if (searcher(&shelf, myLib.collection, &myLib) == 1) {
printf("The book %s is currently in the library.\n\n", shelf.title);
} else {
printf("The book %s is NOT currently in the library.\n\n", shelf.title);
}
} else if (instruct == 4) {
author_list(ifp, myLib.collection, &shelf, &myLib);
} else {
genre_list(ifp, myLib.collection, &shelf, &myLib);
}
To...
Code:
switch( instruct )
{
case 1: addBook(ifp, &myLib, shelf); break;
case 2: removeBook(ifp, &myLib, &shelf); break;
case 3:
{
int k = 0;
fscanf(ifp, "%s", &shelf.title[k]);
if (searcher(&shelf, myLib.collection, &myLib) == 1) {
printf("The book %s is currently in the library.\n\n", shelf.title);
} else {
printf("The book %s is NOT currently in the library.\n\n", shelf.title);
} break;
case 4: author_list(ifp, myLib.collection, &shelf, &myLib); break;
case 5: genre_list(ifp, myLib.collection, &shelf, &myLib); break;
default: printf( "Invalid instruct: %d\n", instruct );
}
Not always the preferred method, but suits this nicely.
Code:
//Function to add book
void addBook(FILE *ifp, struct library *thislib, struct book b) {
fscanf(ifp, "%s%s%s", &b.title[thislib->num_books], &b.author[thislib->num_books], &b.subject[thislib->num_books]);
copybook(thislib->collection, &b);
thislib->num_books++;
printf("The book %s has been added to the library.\n\n", b.title);
}
I would probably separate my file reading functions from my book adding functions. Also, you seem to have a great deal of that mixed up. You're passing struct book b by value, not pointer, so anything you do here is lost anyway--plus, this is just a single book, not a library. You're getting those confused again.
Quzah.