Basically I must create a library system which prompt user for some books input, then we save the books to file and when we open the program for the second time we can load the books that we have saved before int the linked lists.
Well, my problem is I have no idea how to read from the file and load the books back to the linked list when we run the program for the second time. So that we can access the book from the linked list.
Any suggestion?
Here is my code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct book
{
char title[21]; /* 20 + null-terminator */
char author[21]; /* 20 + null-terminator */
char status[21];
struct book* next;
}BOOK;
void display_lists( const BOOK* ptr);
void save_book(BOOK* ptr);
BOOK* load_data(BOOK* book)
BOOK* add_books(void);
/*BOOK* find_nth_book( BOOK* ptr, int n);
BOOK* add_book_at_nth( BOOK* ptr, BOOK* new, int n);
BOOK* delete_nth_book( BOOK* ptr, int n, int* success_flag);*/
int main()
{
BOOK *start;
start = add_books();
save_book(start); /* save book list to file */
display_lists(start);
return 0;
}
/* add_book allocates run-time storage for nodes. It builds
the linked list and stores user-supplied title and author in
the members of the nodes. It returns a pointer to the first
such node */
BOOK* add_books(void)
{
BOOK *current, *first;
char ans[2];
/* allocate first node */
current = first = malloc( sizeof(BOOK) );
/* store title of first book */
printf( "\nPlease enter book's title: \n" );
gets(current -> title);
printf( "\nPlease enter book's author: \n" );
gets(current -> author);
strcpy( current -> status, "available");
/* prompt user about another book */
printf( "\n\nAdd another? (y/n)\t" );
gets(ans);
/* Add book to list until user signals halt. */
while ( *ans != 'n' && *ans != 'N')
{
/* try to allocate another book node */
if ( ( current -> next =
malloc( sizeof ( BOOK ) ) ) == NULL )
{
printf( "Out of memory\nCan't add more books\n" );
return first;
}
current = current -> next;
/* store name of next book */
printf( "\nPlease enter book's title: \n" );
gets(current -> title);
printf( "\nPlease enter book's author: \n" );
gets(current -> author);
strcpy( current -> status, "available");
/* prompt user about another book */
printf( "\nAdd another(y/n)? " );
gets(ans);
}
/* set link in last node to NULL */
current -> next = NULL;
return first;
}
/* display_lists steps through the linked list pointed to by
ptr and prints the member name in each node as well as the
position of the node in the list */
void display_lists( const BOOK* ptr)
{
printf( "\n\n" );
printf( "%-15s\t%-15s\t%-15s\n", "Book Title", "Author", "Status");
while( ptr != NULL )
{
printf( "%-15s\t%-15s\t%-15s\n", ptr->title, ptr->author, ptr->status);
ptr = ptr->next;
}
}
/* Save book list to book_list.dat */
void save_book(BOOK* ptr)
{
FILE *fptr;
fptr = fopen ("book_list.txt", "a");
while (ptr!=NULL)
{
fputs(ptr->title, fptr);
fputs("\t\t", fptr);
fputs(ptr->author, fptr);
fputs("\t\t", fptr);
fputs(ptr->status, fptr);
fputc('\n', fptr);
ptr = ptr->next;
}
fclose(fptr);
}
/* Open book_list.txt and read the file and store it in the linked list */
BOOK* load_data(BOOK* book)
{
FILE *fptr;
fptr = fopen ("book_list.txt", "rb");
/* Function implementation */
}