I'm trying to write a program to act as a spellchecker. I have dictionary file (attached) which i'm trying to scan into an array to be stored, and then scan through another file and compare these words to the array. At the moment, i'm stuck on copying the buffer array i have with the dictionary words in into the dictionary array. I get segmentation faults (using unix). The only thing i can think is wrong is taht i think the program is scanning in \n at teh end of each word, but i'm not sure. The code is below...
Any ideas on how to get this working would be great, and quick responses if pos as this is in tomorrow! (i got held up on another prog involving matrices!).
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*void de_allocate(char* dict_store, int dict_words)
{
int i;
for (i=0; i<=dict_words; i++)
free(dict_store[i]);
free(dict_store);
}*/
int main(void)
{
char buffer[100];
char* dict_store;
int dict_words=0, word_length,i;
FILE* in_file;
if((in_file=fopen("dict.txt","r"))==NULL)
printf("File open failed\n");
while(1)
{
if(fgets(buffer,100,in_file)==NULL) break;
{
printf("test 1\n");
word_length=strlen(buffer);printf("test 2\n");
dict_store = (char*)malloc(word_length*sizeof(char));printf("test 3\n");
for(i=0;i<=dict_words; i++)printf("test 4, dict_words=%d,i=%d\n",dict_words,i);
{
strcpy(dict_store[dict_words],buffer);printf("test 5\n");
printf("%s\n",dict_store[dict_words]);printf("test 6\n");
}
dict_words++;
}
}
fclose(in_file);
return 0;
}