here's my code.
#include <stdlib.h>
#include <stdio.h>
int main() {
//Vars
FILE *fp;
char word[9999],
*arrayOfWords[9999];
int wordCount = 0, i;
//Actions
fp = fopen("data.txt", "r");
if(fp != NULL) {
while(!feof(fp)) {
fscanf(fp, "%s", word);
arrayOfWords[wordCount] = word;
wordCount++;
}
for(i = 0; i < wordCount; i++) {
printf("%s \n", arrayOfWords[i]);
}
puts("");
} else {
puts("Cannot read the file!");
}
return 0;
}
I am trying to read some data from a text file and store it into an array. Everything is fine while I'm in the loop, but when I get out of there, any value of any index in my array is filled with the last word of the file. Could anyone help me find out mistakes I am doing?
Data file:
Hello there, this is a new file.
Result:
file.
file.
file.
file.
file.
file.
file.
file.
Any help would be appreciated!