I am trying to create a program that reads a file called words.dat that contains a word-list of 20 words separated by white-space and tells the number of matched words. However the word cannot be any longer than 17 characters. The matched words are case sensitive so the word "Ninja" and "ninja" would not match. However I am having a difficult time getting my function1() to work correctly.
Here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define WORDS 20
#define LENGTH 17
void intro_msg (void);
char function1 (char [WORDS] [LENGTH]);
void goodbye_msg (void);
int main( void )
{
char word_array [WORDS] [LENGTH];
intro_msg ( ) ;
function1 (word_array);
goodbye_msg ( ) ;
return ( 0 ) ;
}
void intro_msg (void)
{
printf( "\n Welcome user.\n");
return ;
}
char function1 (char word_array[WORDS] [LENGTH])
{
FILE *wordsfile ;
wordsfile = fopen("words.dat", "r");
if (wordsfile == NULL)
printf("\n words.dat was not properly opened.\n");
else
{
printf ("\n words.dat is opened in the read mode.\n\n");
fscanf (wordsfile, "%s", word_array );
while ( !feof (wordsfile) )
{
printf (" %s \n", word_array);
fscanf (wordsfile , "%s", word_array );
}
fclose(wordsfile);
}
return (word_array [WORDS] [LENGTH]);
}
void goodbye_msg (void)
{
printf ( "\n Thank you for using this program. GoodBye. \n\n " ) ;
return ;
}
The overall program is supposed to
Create an array of 20 character strings, each string should be a maximum of 17 characters
Populate each element of the array of strings from a file named words.dat (function1( ) )
Methodically traverse the array looking for identical words, these
words must match exactly, including case (function2( ) )Display to the screen the contents of the array of strings
(words.dat) file and the number of identical pairs of words
(function3( ) )
What can I do to fix function1 so that it accomplishes the task of populating each element of the array of strings from the file specified?
Current sample output:
Welcome user.
words.dat is opened in the read mode.
Ninja
DragonsFury
failninja
dragonsrage
leagueoflegendssux
white
black
red
green
yellow
green
leagueoflegendssux
dragonsfury
Sword
sodas
tiger
snakes
Swords
Snakes
Thank you for using this program. GoodBye.
- Note the words listed are contained in the word.dat file.