i want to put my search result(from a linked list) into a linked list from a file
here is my code the problem i think is the loop
in the search fuction
but i don't see the problem
Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#define BUFSIZE 255
struct dbline{
char author[BUFSIZE];
char title[BUFSIZE];
char edition[BUFSIZE];
char isbn[10];
char publisher[BUFSIZE];
char comment[BUFSIZE];
struct dbline *next_rec;
};
typedef struct dbline LIST;
typedef LIST *LINK;
void clear_kb(void)
{
char junk[BUFSIZE];
fgets(junk,BUFSIZE,stdin);
}
LINK search(LINK first)
{
LINK tmp_rec = NULL;
LINK cur_rec = NULL;
char *p;
char tmp_buf[BUFSIZE];
char search_buf[20];
puts("Enter the author's name that you want to search");
fgets(search_buf,20,stdin);
p = strchr(search_buf,'\n');
if ( p != NULL)
*p = '\0';
clear_kb();
tmp_rec = (LINK) malloc(sizeof(LIST));
cur_rec = first;
while ( cur_rec != NULL )
{
strcpy(tmp_buf,cur_rec->author);
if(strstr(tmp_buf,search_buf) != NULL)
{
if (tmp_rec == NULL)
{
tmp_rec = cur_rec;
first = tmp_rec;
}
else
{
tmp_rec ->next_rec = cur_rec;
}
}
tmp_rec->next_rec = NULL;
}
return first;
}
FILE *open_file_to_read(FILE *fp ,char *filename)
{
if((fp = fopen(filename ,"r")) == NULL)
{
fprintf(stderr ,"error opening file");
exit(-1);
}
return fp;
}
LINK read_to_list(FILE *fpr, LINK first)
{
LIST tmp_rec ;
LINK new_rec = NULL;
LINK cur_rec = NULL;
while(fgets(tmp_rec.author, BUFSIZE, fpr) && fgets(tmp_rec.title, BUFSIZE, fpr) && fgets(tmp_rec.edition, BUFSIZE, fpr) && fgets(tmp_rec.isbn, BUFSIZE, fpr) && fgets(tmp_rec.publisher, BUFSIZE, fpr) && fgets(tmp_rec.comment, BUFSIZE, fpr) != NULL)
{
new_rec = (LINK) malloc(sizeof(LIST));
*new_rec = tmp_rec;
if (cur_rec == NULL)
{
cur_rec = new_rec;
first = new_rec;
}
else
{
cur_rec->next_rec = new_rec;
cur_rec=new_rec;
}
new_rec->next_rec = NULL;
}
return first;
}
void print_all(LINK first)
{
LINK cur_rec = NULL;
cur_rec = first;
while (cur_rec != NULL)
{
printf("\nauthor: %s", cur_rec->author);
printf("\ntitle: %s", cur_rec->title);
printf("\nedition: %s", cur_rec->edition);
printf("\nisbn: %s", cur_rec->isbn);
printf("\npublisher: %s", cur_rec->publisher);
printf("\ncomment: %s", cur_rec->comment);
cur_rec = cur_rec->next_rec;
}
}
any suggestion would be nice