here is 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 in an array. Everything is fine as long as I am in the loop, but when I exit from there, any value of any index in my array is filled with the last word of the file. Can someone help me find out the mistakes that I make?
Data file:
Hello there, this is a new file.
Result:
file. file. file. file. file. file. file. file.
Any help would be appreciated!
iNfas source share