Thread: trouble reading sentences into a single linked list from a file

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

    trouble reading sentences into a single linked list from a file

    i am trying to write a database for my books
    burt when i want to read from a file into my linked list
    it breaks everything up in words
    the file looks like this
    stevens
    network programming
    second
    1-123-456
    maddison
    good
    stevens
    tcp/ip illustrated
    second
    0-123-456
    maddison
    good
    i use fscanf
    i tried to replace it with fread and fgets but no succes
    here is the code of my print fuctions

    Code:
    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;
    
    
    
    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(fscanf(fpr, "%s\n%s\n%s\n%s\n%s\n%s\n", tmp_rec.author, tmp_rec.title, tmp_rec.edition, tmp_rec.isbn ,tmp_rec.publisher, tmp_rec.comment)!= EOF)
    	{
    		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;
    	}
    there are more functions i think these are the ones
    messing things up
    can anybody tell me what i am doing wrong

  2. #2
    Registered User
    Join Date
    Dec 2002
    Posts
    18
    allready got it
    with a kot of fgets() &&
    and it worked

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting linked list please help with CODE
    By scarlet00014 in forum C Programming
    Replies: 3
    Last Post: 09-27-2008, 11:24 PM
  2. help me debug this linked list !
    By Dark Angel in forum C Programming
    Replies: 6
    Last Post: 04-18-2008, 02:10 PM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 06:13 AM