c - segmentation fault in my binary search tree code while file handling -
the code error free not run. have applied various print statements near file handling. problem appears there. can see if find problem?
my input file has data in format:
insert "tab>" "name" "tab""idno" "tab" "department""newline" delete "tab""idno""newline" find"tab"idno"newline" code:
/*bst.h*/ typedef struct{ unsigned int id; char name[20]; char dep[10]; }studrec; struct bst; typedef struct bst bintree; typedef struct bintree *bst; struct bintree{ studrec srec; bst left; bst right; }; /* bstmain.c*/ #include "bstops.h" #include <stdio.h> #include <stdlib.h> #include <string.h> int main(){ int s; int key,i; file *f; char a[4][20]; f=fopen("input.txt","r"); if(f==null) printf("error"); else printf("no error"); char ch=getc(f); studrec rec; bst bt=createemptybst(); do{ for(i=0;ch!='\n';i++) fscanf(f,"%s",a[i]); if(a[0]=="insert") s=1; else if(a[0]=="delete") {s=2; key=atoi(a[1]);} else if(a[0]=="find"){ s=3; key=atoi(a[1]);} printf("to print list, press 4\n exit,press 5.\n"); scanf("%d",&s); switch(s){ case 1: strcpy(rec.name,a[1]); rec.id=atoi(a[2]); strcpy(rec.dep,a[3]); bt=insertinbst(bt,rec); break; case 2: bt=deletefrombst(bt,key); break; case 3: rec=findinbst(bt,key); case 4: inordertraversal(bt); break; case 5: printf("\nterminating!!!\n"); break; default: printf("\ninvalid option!! \n"); break;} ch=getc(f); }while(ch!=eof||s==5); fclose(f); } /*bstops.c*/ #include "bstops.h" #include <string.h> #include <stdio.h> #include <stdlib.h> bst createemptybst(){ bst root; root=(bst)malloc(sizeof(bst)); // root->srec=(studrec *)malloc(sizeof(studrec)); root->srec.id=0; strcpy(root->srec.name,"/"); strcpy(root->srec.dep,"/"); root->left=null; root->right=null; //root=null; return root; } bst insertinbst(bst bt,studrec rec){ bst bt1=createemptybst(); bt1=bt; while(1){ /* if(bt==null){ bt->srec=rec; bt->left=null; bt->right=null; break;} */ if(bt->srec.id==rec.id) { return bt; break; } else if(rec.id<bt->srec.id){ if(bt->left==null){ bt->left=createemptybst(); bt=bt->left; bt->srec=rec; break; } else bt=bt->left; } else{ if(bt->right==null){ bt->right=createemptybst(); bt=bt->right; bt->srec=rec; break; } else bt=bt->right; } } printf("inserted\n"); return bt1; } void inordertraversal(bst bt){ if(bt!=null){ inordertraversal(bt->left); if(bt->srec.id!=0) printf("\t id:%d\n name:%s\n deparment:%s\n",bt->srec.id,bt->srec.name,bt->srec.dep); inordertraversal(bt->right); } } studrec findinbst(bst bt,int id){ bst temp=createemptybst(); if(bt==null||bt->srec.id==0) return temp->srec; if(id>bt->srec.id) return findinbst(bt->right,id); else if(id<bt->srec.id) return findinbst(bt->left,id); else return bt->srec; } bst deletefrombst(bst bt,int id){ bst q; if(bt==null||bt->srec.id==0){ printf("\nelement not found\n"); } else if(id>bt->srec.id) bt->right=deletefrombst(bt->right,id); else{ if(bt->right && bt->left){ q=findmin(bt->right); bt->srec=q->srec; bt->right=deletefrombst(bt->right,q->srec.id); } else{ q=bt; if(bt->left==null) bt=bt->right; else if(bt->right==null) bt=bt->left; free(q); } } return bt; } bst findmin(bst bt){ if(bt==null||bt->srec.id==0){ return null; } if(bt->left) return findmin(bt->left); else return bt; } int getheight(bst bt){ int h,lh,rh; h=lh=rh-0; if(bt->left!=null) lh=(1+getheight(bt->left)); else if(bt->right!=null) rh=(1+getheight(bt->right)); h=lh>rh?lh:rh; return(h); }
the crash caused invariant loop:
for(i=0;ch!='\n';i++) fscanf(f,"%s",a[i]); the stop condition never true, end overwriting array a cause segmentation fault.
here fix proposal. need update according needs
int getheight(bst bt) { int h, lh, rh; h = lh = rh = 0; if (bt->left != null ) lh = (1 + getheight(bt->left)); else if (bt->right != null ) rh = (1 + getheight(bt->right)); h = lh > rh ? lh : rh; return (h); } int main(int argc, char *argv[]) { int s; int key = 0, i; file *f; char line[200]; char options[5][200]; f = fopen(argv[1], "r"); if (f == null ) { printf("error"); return -1; } printf("no error"); char ch = fgetc(f); studrec rec; bst bt = createemptybst(); { (i = 0; ch != '\n' && ch != eof; i++) { line[i] = ch; ch = fgetc(f); } if (eof == ch) { printf("to print list, press 4\n exit,press 5.\n"); scanf("%d", &s); } if (!strncmp(line, "insert", strlen("insert"))) { s = 1; // todo parse line string options sprintf(line, "insert %s %s %s", options[0], options[1], options[2]); } else if (!strncmp(line, "delete", strlen("delete"))) { s = 2; key = atoi(line + strlen("delete") + 1); } else if (!strncmp(line, "find", strlen("find"))) { s = 3; key = atoi(line + strlen("find") + 1); } switch (s) { case 1: strcpy(rec.name, options[0]); rec.id = atoi(options[1]); strcpy(rec.dep, options[2]); bt = insertinbst(bt, rec); break; case 2: bt = deletefrombst(bt, key); break; case 3: rec = findinbst(bt, key); break; case 4: inordertraversal(bt); break; case 5: printf("\nterminating!!!\n"); ch = eof; // break {} while() loop break; default: printf("\ninvalid option!! \n"); break; } } while (ch != eof || s == 5); fclose(f); return 0; }
Comments
Post a Comment