putting search result into a linked list

This is a discussion on putting search result into a linked list within the C Programming forums, part of the General Programming Boards category; i want to put my search result(from a linked list) into a linked list from a file here is my ...

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    18

    putting search result into a linked list

    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

  2. #2
    Registered User
    Join Date
    Dec 2002
    Posts
    18
    i don;t get it what you mean with repairing this is my first program with single linked list
    i want to make a temporay node that has as only purpose printing it for search result or deleting

  3. #3
    Registered User
    Join Date
    Dec 2002
    Posts
    18
    but i want to copy my data to a tmp node and i want to
    return the first node of my data to print
    thx for your quick reply

  4. #4
    Registered User
    Join Date
    Dec 2002
    Posts
    18
    ah well i willi give it a try find it all very confusing
    thx again for the quick info

    another question is there a possiblity of having an infinte loop
    at
    while ( cur_rec != NULL )
    Last edited by jetfreggel; 03-03-2003 at 12:57 PM.

  5. #5
    Registered User
    Join Date
    Dec 2002
    Posts
    18
    forgot cur_rec=cur_rec->next_rec
    was caught in an infinite loop and know the
    first problem is gone
    thx for your help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List Not Saving Value as Int
    By bar338 in forum C Programming
    Replies: 4
    Last Post: 05-04-2009, 08:53 PM
  2. Sorting linked list please help with CODE
    By scarlet00014 in forum C Programming
    Replies: 3
    Last Post: 09-28-2008, 12:24 AM
  3. Need help sorting a linked list. Beginner
    By scarlet00014 in forum C Programming
    Replies: 1
    Last Post: 09-27-2008, 07:16 PM
  4. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 06:13 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 11:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21