これが私のコードです。
#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;
}
テキスト ファイルからデータを読み取って配列に格納しようとしています。ループ中はすべて問題ありませんが、そこから抜け出すと、配列内の任意のインデックスの値がファイルの最後の単語で埋められます。私がやっている間違いを見つけるのを手伝ってくれる人はいますか?
データファイル:
Hello there, this is a new file.
結果:
file.
file.
file.
file.
file.
file.
file.
file.
どんな助けでも大歓迎です!