I'm trying to store data from a file (words line by line) maximum length is 16
when I run the code it only stores the last word on the file and prints it (number of all the words in the file) times.
if the file contains 10 words and the last word is test, the output is test 10 times
when I tried while loop to scan the file I got garbage data
here is my code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
FILE *file;
file = fopen("words.txt", "r");
char word[20];
int words_count=0; // count the words in the file
while (fscanf(file, "%s", word) != EOF)
{
words_count++;
}
printf("%d", words_count); // i get the right number of words in the file at this step
char **list_of_words = malloc (words_count * sizeof(char *)); //allocate memory to store enough pointers to charecters
int length;
for (int i=0; i<words_count; i++)
{
fscanf(file, "%s", word);
length = strlen(word);
word[length+1] = '\0';
list_of_words[i] = malloc (length+1 * sizeof(char)); //allocate memory for each word
list_of_words[i] = word; //I used strcpy(), but I got the same output
}
for (int i=0; i<words_count; i++)
{
printf("%s\n", list_of_words[i]); //print the words
}
fclose(file);
}