So I tried to get the codewords from a file and put them into my words array. When I print them inside the for loop I get all the codewords from the .txt file but when I try to print one of the elements of my array, it always gives the last line from the text file. How can I fix this?
This is my text file's content: ape car big 123 book door epic band test apple catch super level small basket abroad action carbon program buzzcut jimjams muzzles puzzles dazzled football baseball absolute campaign casualty division quizzical twizzling bedazzled fuzziness maximizer strawberry friendship motivation everything ==> each word in an another line.
char *words[0][39];
int main() {
FILE *codewordsPtr;
if((codewordsPtr = fopen("codewords.txt","rb")) == NULL){
printf("file could not be opened");
}
char bum[12];
int wordList = 0;
for(int i=0; i<20; i+=5,wordList++){
fseek(codewordsPtr, i, SEEK_SET);
fgets(bum, 12, codewordsPtr);
words[0][wordList] = bum;
printf("%s" ,words[0][wordList]);
}
for(int i=20; i<50; i+=6,wordList++){
fseek(codewordsPtr, i, SEEK_SET);
fgets(bum, 12, codewordsPtr);
words[0][wordList] = bum;
printf("%s" ,words[0][wordList]);
}
for(int i=50; i<85; i+=7,wordList++){
fseek(codewordsPtr, i, SEEK_SET);
fgets(bum, 12, codewordsPtr);
words[0][wordList] = bum;
printf("%s" ,words[0][wordList]);
}
for(int i=85; i<117; i+=8,wordList++){
fseek(codewordsPtr, i, SEEK_SET);
fgets(bum, 12, codewordsPtr);
words[0][wordList] = bum;
printf("%s" ,words[0][wordList]);
}
for(int i=117; i<171; i+=9,wordList++){
fseek(codewordsPtr, i, SEEK_SET);
fgets(bum, 12, codewordsPtr);
words[0][wordList] = bum;
printf("%s" ,words[0][wordList]);
}
for(int i=171; i<231; i+=10,wordList++){
fseek(codewordsPtr, i, SEEK_SET);
fgets(bum, 12, codewordsPtr);
words[0][wordList] = bum;
printf("%s" ,words[0][wordList]);
}
for(int i=231; i<286; i+=11,wordList++){
fseek(codewordsPtr, i, SEEK_SET);
fgets(bum, 12, codewordsPtr);
words[0][wordList] = bum;
printf("%s" ,words[0][wordList]);
}
for(int i=286; i<334; i+=12,wordList++){
fseek(codewordsPtr, i, SEEK_SET);
fgets(bum, 12, codewordsPtr);
words[0][wordList] = bum;
printf("%s" ,words[0][wordList]);
}
printf("\n\n\n");
printf("%s", words[0][5]);
fclose(codewordsPtr);
return 0;
}
This is my output: ape car big 123 book door epic band test apple catch super level small basket abroad action carbon program buzzcut jimjams muzzles puzzles dazzled football baseball absolute campaign casualty division quizzical twizzling bedazzled fuzziness maximizer everything
everything Process returned 0 (0x0) execution time : 0.015 s Press any key to continue.