We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
i have written a code for a kind of spellchecker . it reads words frm a input file and computes it hash value and then lookupc in hash table of dictionary of approx 11000 words .but it terminates after certain point in dictionary and gives following access violation error
#include<cstdlib> #include<cstdio> #include<cctype> #include<cstring> void main() { FILE *fp; FILE *fpp; FILE *fpt; char ch; fopen_s(&fp,"dictionary2.txt","r+"); fopen_s(&fpp,"input.txt","r+"); fopen_s(&fpt,"output.txt","w"); int j=0; unsigned hash=0; int i=0; char h[16][16]; char temp[16]; while((ch=fgetc(fp))!=EOF) { while(ch!='\n'){ hash=5*hash + tolower(ch); *(temp+j)=ch;//temp[j]=ch; ch=fgetc(fp); j++; } *(temp+j)='\0'; hash=hash%17; strcpy_s(*(h+hash),temp); hash=0; j=0; for(int i=0;i<16;i++) temp[i]='\0'; } for(j=0;j<16;j++) fprintf(fpt,"\n%d\t%s",j,*(h+j)); while((ch=fgetc(fpp))!=EOF) { while(ch!='\n'&&ch!='\t'&&ch!='.'&&ch!=','&&ch!=' '&&ch!='\0'){ hash=5*hash + tolower(ch); *(temp+j)=ch; ch=fgetc(fpp); j++; } temp[j]='\0'; if(hash!=0){ hash=hash%17; if((strcmp(temp,*(h+hash)))==0){ fprintf(fpt,"\n%s\t%d\tfound",temp,hash); } hash=0; j=0; for(int i=0;i<16;i++) temp[i]='\0'; } } fclose(fp); fclose(fpp); fclose(fpt); }
you hardly provide useful data - like, where does the failure occur?
a few points, though:
* your program is sensitive to invalid input - if there is no '\n' after 17 characters,
*(temp+j)=ch;//temp[j]=ch;
will cause a buffer overflow, and possible an access violation. * the same goes for
while(ch!='\n'&&ch!='\t'&&ch!='.'&&ch!=','&&ch!=' '&&ch!='\0'){...