I have a word file with 3 words. I need to read each word into an array. I tried doing this with fscanf(), but it doesn’t work. What am I doing incorrectly?
#include <stdio.h>
void main(){
char words_array[80];
FILE *dictionary;
dictionary = fopen("dictionary.txt", "r");
fscanf (dictionary, "%s", words_array);
printf("The content of words_array is: %s, %s, %s, \n", words_array[0], words_array[1], words_array[2]);
}
I get the following error when I try to compile:
warning: format ‘%s’ expects argument of type ‘* char’ but argument has type ‘int’
The dictionary.txt file is as follows:
apple
orange
bananna
Thanks all!
Assuming you have only one word (fruit) in one line (and < 80 characters long) in the
dictionary.txt, the following should work!Output:
Adding an alternative answer as per request from the OP author.
Output