problem with segmentation fault
Hi..everyone..i had trying to solve this problem but i really can't because i don't have any experience with C core dumped.Below is a program that can be compile but after exceution i get Segmentation fault (core dumped) with gcc (cygwin) compiler. Can anyone give me some piece of advice or tips.Thanks alot.
Code:
typedef struct bookStruct
{
int id;
char title[TITLE_LEN + 1];
char author[AUTHOR_LEN + 1];
int year;
float price;
int count;
struct bookStruct* next;
} BookType;
typedef struct bookListStruct
{
BookType* head;
int count;
} BookListType;
BookType *b;
/* Function prototypes. */
void loadData(BookListType*, char*);
void displayBooks(BookListType*);
//int main(void)
int main(int argc, char *argv[])
{
BookListType* booklist;
int i;
FILE *bookRead;
char line[LINESIZE] , *word;
if ( argc != 2 )
{
fprintf ( stderr, "Usage: %s <readfile1>\n", argv[0] );
exit ( EXIT_FAILURE );
}
if((bookRead = fopen( argv[1], "r" )) == NULL)
{
fprintf(stderr, "cat: can't open %s\n", argv[1]);
}
loadData(booklist, argv[1]);
displayBooks(booklist);
fclose ( bookRead );
return EXIT_SUCCESS;
}
void loadData(BookListType* booklist, char* booksFile)
{
char *delimeter = ":";
char *token, *bookPtr;
char bookWord[100];
BookType *b;
FILE *bfile;
/* fopen opens the file; exits if file cannot be opened */
if ( ( bfile = fopen( booksFile, "r" ) ) == NULL )
{
fprintf(stderr,"***> Open error reading input file %s", booksFile);
exit(-1);
}
/* read all records from file (until eof) */
while(!feof(bfile))
{
while (bookPtr != NULL)
{
if(fgets(bookWord,sizeof(bookWord),bfile)!=NULL)
{
if (b = (BookType*)malloc(sizeof(BookType)))
{
/* copy the data from BookType to linked list */
bookPtr = strtok(bookWord,delimeter);
b->id=atoi(bookPtr);
strcpy(b->title, bookPtr);
strcpy(b->author, bookPtr);
b->year = atoi(bookPtr);
b->price = atof(bookPtr);
b->count = atoi(bookPtr);
}
bookPtr = strtok(NULL, delimeter);
}
b->next = booklist->head;
booklist->head = b;
booklist->count++;
}
}
fclose(bfile);
}
void displayBooks(BookListType* booklist)
{
printf("ID Title\t\t\t Author\t Year\tPrice\tCount\n");
printf("-- ----------------------------- ------------ ----\t-------\t-----\n");
while(booklist->head!=NULL)
{
printf("%2d %-32s%-12s%6d\t%2c%5.2f\t%4d\n", booklist->head->id,
booklist->head->title, booklist->head->author, booklist->head->year,'$',
booklist->head->price,booklist->head->count);
booklist->head = booklist->head->next;
}
}