I am trying to read a file of 500k+ words that will be entered into a binary search tree. First I am just trying to read these words into an array of strings but it does not seem to be working. Below is the code I wrote but after spending an hour figuring out why every entry is being set to the very last word in the .txt I am at a loss. Below the code is the output, I don't need the output but I was using it as an attempt to debug the code or to see what is going wrong... but it did not help.
Code:int main() { FILE *file; int numWords=0, i, j; char fileName[30], wordString[30]; char **wordArray; printf("Enter the name of the file:"); scanf("%s", fileName); file = fopen(fileName, "r"); if(!file) { printf("File does not exist. Program terminated"); return 1; } else { while(!feof(file)) { fgets(wordString, 30, file); numWords++; } rewind(file); wordArray = (char **)checked_malloc(numWords*sizeof(char*)); for(i = 0; fgets(wordString, 30, file); ++i) { printf("%d %s", i, wordString); wordArray[i] = (char*)checked_malloc(strlen(wordString)*sizeof(char)); wordArray[i] = wordString; } fclose(file); } printf("i=%d words=%d \n", i, numWords-1); for(j = 0; j < numWords-1; j++) printf("%d: %s ", j, wordArray[j]); free(wordArray); return 0; }As the output shows, every string in the array wordArray is set to the last word in the txt. If anyone could show me what part I am doing wrong it would be greatly appreciated.Enter the name of the file:small.txt
0 cat
1 mouse
2 bear
3 deer
4 haha
5 newbie
6 llama
7 dog
8 animal
9 rocket
10 ship
11 blanket
12 snake
13 ohnoes
14 wtfbbq
i=15 words=15
0: wtfbbq
1: wtfbbq
2: wtfbbq
3: wtfbbq
4: wtfbbq
5: wtfbbq
6: wtfbbq
7: wtfbbq
8: wtfbbq
9: wtfbbq
10: wtfbbq
11: wtfbbq
12: wtfbbq
13: wtfbbq
14: wtfbbq
Thanks.




