Thread: problem printf after inserting single list from file

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

    problem printf after inserting single list from file

    hi everyone
    i am writing a book datzabase(my first)

    the trouble is when i want to check if the funcition finds the same isbn number it wrecks my linked list
    an example

    org isbn 1-234-56789-0

    i want to make 1234567890 when i use input directly from the keyboard no problem
    but when i read the isbn-number from a file
    i get instead of 1-234-56789-0
    becomming 123456789! my last number is changed into a exclamtion point
    any one got any ideas here is the code[
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<ctype.h>
    
    #define BUFSIZE 80
    
    struct dbline{
    	char author[BUFSIZE];
    	char title[BUFSIZE];
    	char edition[BUFSIZE];
    	char isbn[15];
    	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_isbn(LINK first)
    {
    	LINK tmp_rec = NULL;
    	LINK cur_rec = NULL;
    	LINK head = NULL;
    	char *p;
    	char ch;
    	char *search_buf;
    	char *search_buf1;
    	char *tmp_buf;
    	char *tmp_buf1;
    	long fin_isbn_num;
    	long fin_isbn_tmp;
    	int i = 0;
    	int ii = 0;
    	search_buf = (char *) malloc(15 * sizeof(char));
    	search_buf1 = (char *) malloc(12 * sizeof(char));
    	tmp_buf = (char *) malloc(15 * sizeof(char));
    	tmp_buf1 = (char *) malloc(12 * sizeof(char));
    	puts("Enter the isbn number that you want to search");
    	fgets(search_buf,16,stdin);
    /*	p = strchr(search_buf,'\n');
    		if ( p != NULL)
    			*p = '\0';*/
    	clear_kb();
    	cur_rec = first;
    	while(search_buf[i] != '\n')
    	{
    			ch = search_buf[i];	
    			if(search_buf[i++] != '-')
    			{	
    				search_buf1[ii++] = ch;
    			}
    	}
    	printf("%s\n", search_buf1);
    	puts("\n");
    	while (cur_rec != NULL)
    	{
    	printf("%s\n", cur_rec->isbn);
    	puts("\n");
    
    		i = 0;
    		ii = 0;
    		strcpy(tmp_buf,cur_rec->isbn);
    		while(tmp_buf[i] != '\n')
    		{
    			ch = tmp_buf[i];	
    			if(tmp_buf[i++] != '-')
    			{	
    				tmp_buf1[ii++] = ch;
    			}
    		}
    		printf("%s\n", tmp_buf1);
    		fin_isbn_num = atol(search_buf1);
    		fin_isbn_tmp = atol(tmp_buf1);
    
    		printf("%ld\n", fin_isbn_num);
    		printf("%ld\n", fin_isbn_tmp);
    
    
    		if(fin_isbn_num == fin_isbn_tmp) 
    		{
    			if(head == NULL)
    			{
    				tmp_rec = cur_rec;
    				head = tmp_rec;
    			}
    		}
    		cur_rec = cur_rec->next_rec;
    	}
    	return head;
    }
    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;
    	}
    	printf("%s", first->author);
    	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;
    	}
    }
    int menu_search(void)
    {
    	int answer;
    	puts("1:Author");
    	puts("2:Title");
    	puts("3:Isbn");
    	puts("4:Publisher");
    	puts("5:Comment");
    	scanf("%d", &answer); /*veranderen in short*/
    	clear_kb();
    	return answer;
    }
    char menu(void)
    {
    	char answer;
    	puts("New : n");
    	puts("Add new line: a");
    	puts("Modifya new line: m");
    	puts("Delete: d");
    	puts("Print: p");
    	puts("Quit: q");
    	scanf("%c", &answer);
    	clear_kb();
    	return answer;	
    }
    
    int main (void)
    {	
    	LINK first = NULL;
    	LINK new_rec = NULL;
    	new_rec = (LINK)malloc(sizeof(LIST));
    	char answer_m;
    	char filename[BUFSIZE];
    	char *p;
    	FILE *fp ;
    	FILE *fpr;
    	int answer_search = 0;
    	if(!new_rec)
    	{
    		printf("\nUnable to alloctte memory!\n");
    		exit(1);
    	}
    	answer_m = menu();
    	switch(answer_m)
    	{
    		case 'p':
    		{
    			puts("Enter the filename:");
    			fgets(filename,BUFSIZE,stdin);
    			p = strchr(filename,'\n');
    			if ( p != NULL)
    				*p = '\0';
    			clear_kb();
    			fpr = open_file_to_read(fp, filename);
    	first = read_to_list(fpr, first);
    			fclose(fpr);
    			while(1)
    			{
    				answer_search = menu_search();
    				switch(answer_search)
    				{
    				case 3:
    				{
    					first = search_isbn(first);
    					print_all(first);
    					exit(-1);	
    		 		}
    				}
    			}
    			break;
    		}
    		case 'q':
    		{
    			exit(1);
    		}
    	}
    		return 0;
    }
    thanks in advance

  2. #2
    Registered User
    Join Date
    Dec 2002
    Posts
    18
    i think i got thank you for your advice
    Code:
    void strip_string(char *strip_buf)
    {
    	char tmp_buf[12];
    	int i = 0;	
    	int ii = 0;
    	char ch;
    	printf("%s", strip_buf);
    	while(strip_buf[i++] != '\n')
    	{
    		ch = strip_buf[i];
    		if ( ch != '-' )
    		{
    			tmp_buf[ii++] = ch;	
    		}
    	}
    	tmp_buf[ii] = '\0';
    
    }	
    	
    		 	
    
    LINK search_isbn(LINK first)
    {
    	LINK tmp_rec = NULL;
    	LINK prev_rec = NULL;
    	LINK cur_rec = NULL;
    	LINK head = NULL;
    	char *p;
    	char search_buf[15];
    	char tmp_buf[15];
    	puts("Enter the isbn number that you want to search");
    	fgets(search_buf,15,stdin);
    	clear_kb();
    	cur_rec = first;
    	printf("%s\n" ,search_buf);
    	strip_string(search_buf);	
    	printf("%s\n" ,search_buf);
    	while (cur_rec != NULL)
    	{
    
    		strcpy(tmp_buf,cur_rec->isbn);
    		strip_string(tmp_buf);	
    		if (strcmp(tmp_buf,search_buf) == 0)
    		{
    			tmp_rec = (LINK) malloc(sizeof(LIST));
    			*tmp_rec = * cur_rec;	
    			if(head == NULL)
    			{
    			prev_rec = tmp_rec;
    			head = tmp_rec;
    			}
    			else
    			{
    			prev_rec->next_rec = tmp_rec;
    			prev_rec = tmp_rec;
    			}
    		tmp_rec->next_rec = NULL;		
    		}	
    		cur_rec = cur_rec->next_rec;
    	}
    	return head;
    }

  3. #3
    Registered User
    Join Date
    Dec 2002
    Posts
    18
    thx again for your reply
    but i don't get
    sopy like strcpy
    or returning from the function but when i try to assign
    it to a new string my compiler says
    incompatible types in assignment
    i don't ubderstand it i think

  4. #4
    Registered User
    Join Date
    Dec 2002
    Posts
    18
    thx again for your advice

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  2. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 01:21 PM
  3. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  4. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 01:29 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM