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[
thanks in advanceCode:#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; }



LinkBack URL
About LinkBacks


