Following is wrong,
Code:
strcpy( current-> id , id ); /* error without casting */
strcpy( current -> year, year ); /* error without casting */
strcpy( current -> price, price ); /* error without casting */
strcpy( current -> count, price ); /* error without casting */
id,year and count are of type integer and price is float. Use "=" to store values to in it. strcpy() is used to copy char array only.
Also, you need to pass BookType and not BookList Type to loadData() function. If you want to store the count you can return it as a return value of the function.
Code:
booklist.count = loadData( /*Arguments */);
Also, in load data you need to build the linked list of books. Just pass the head pointer to it.
Use fgets() instead of feof() to read check the end-of-file.
Code:
int main()
{
BookType *head=NULL;
BookListType booklist;
const char *booksfile="abc.txt";
const char *salesfile= "xyz.txt";
booklist.count=loadData(&head, booksfile,salesfile);
booklist.head=head;
printf("Total Records loaded: %d\n",booklist.count);
return 0;
}
int loadData(BookType **head,char *booksfile,char *salesfile)
{
int count=0;
BookType *current=NULL;
BookType *last=NULL;
FILE *bp; /*books file pointer */
char line[200]; /*buffer to read from file*/
bp = fopen(booksfile,"r");
if (bp==NULL)
{
printf("Can not open booksfile %s\n",booksfile);
return 0;
}
while ((fgets(line,sizeof(line),bp)) !=NULL)
{
if ((*head)==NULL)
{
/* Build First node */
(*head)=(BookType *)malloc(sizeof(BookType));
if ((*head) == NULL )
{
printf("Malloc failed for (*head) pointer\n");
return 0;
}
sscanf(line,"%d %s %s %d %f %d",
&(*head)->id, (*head)->title,
(*head)->author,&(*head)->year,
&(*head)->price,&(*head)->count);
(*head)->head = getSalesInfo((*head)->title); /*get book sales detail based on title*/
(*head)->next=NULL;
last=(*head);
count++;
} else {
/*Build remaining nodes */
current=(BookType *)malloc(sizeof(BookType));
if (current == NULL )
{
printf("Malloc failed for current pointer\n");
return 0;
}
sscanf(line,"%d %s %s %d %f %d",
&((current)->id), current->title,
current->author,&((current)->year),
&((current)->price),&((current)->count));
current->head = getSalesInfo(current->title); /*get book sales detail based on title*/
current->next=NULL;
last->next=current;
last=current;
count++;
}
}
return count;
}
Also, dont forget to free the memory that you are mallocing.
Write a function to free the linked list, pass the head pointer to it. Just walk through the list and free individual nodes.