0

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.
Nick
  • 183
  • 1
  • 3
  • 15

2 Answers2

2

I'm sorely tempted to drop the greeting functions from the mix, but…

This compiles cleanly, unlike the original. It also allows up to 17 characters plus a terminating null in the string. It uses fscanf() to enforce that limit, too. Note that it does not use feof() — few programs need to. It doesn't overflow the array bounds even if someone provides a file with more than 20 words; nor does it run into problems if the file has fewer words. It doesn't put spaces before newlines.

#include <stdio.h>

#define WORDS 20
#define LENGTH 17

void intro_msg(void);
int  function1(char[WORDS][LENGTH+1]);
void goodbye_msg(void);

int main( void )
{
    char word_array[WORDS][LENGTH+1];

    intro_msg();
    int n = function1(word_array);
    printf("Found %d words\n", n);
    goodbye_msg();

    return(0);
}

void intro_msg(void)
{
    printf("\nWelcome user.\n");
}

int function1(char word_array[WORDS][LENGTH+1])
{
    FILE *wordsfile = fopen("words.dat", "r");
    int i = 0;

    if (wordsfile == NULL)
        printf("\nwords.dat was not properly opened.\n");
    else
    {
        printf("\nwords.dat is opened in the read mode.\n\n");
        for (i = 0; i < WORDS; i++)
        {
            if (fscanf(wordsfile, "%17s", word_array[i]) != 1)
                break;
            printf("   %s\n", word_array[i]);
        }
        fclose(wordsfile);
    }
    return i;
}

void goodbye_msg(void)
{
    printf("\nThank you for using this program. GoodBye.\n\n");
}

This code does not look for duplicates or anything.

Note that your sample data includes words of 18 characters (leagueoflegendssux). You don't say what should happen then. If you need to read the whole line and truncate what's read so it fits, or reject the line, or ... what?

Here's some alternative code, minus greeting functions, that truncates when necessary:

#include <stdio.h>
#include <string.h>

#define WORDS 20
#define LENGTH 17

void intro_msg(void);
int  function1(char[WORDS][LENGTH+1]);
void goodbye_msg(void);

int main(void)
{
    char word_array[WORDS][LENGTH+1];

    int n = function1(word_array);
    printf("Found %d words\n", n);

    return(0);
}

int function1(char word_array[WORDS][LENGTH+1])
{
    FILE *wordsfile = fopen("words.dat", "r");
    int i = 0;

    if (wordsfile == NULL)
        printf("\nwords.dat was not properly opened.\n");
    else
    {
        char line[4096];
        printf("\nwords.dat is opened in the read mode.\n\n");
        for (i = 0; i < WORDS; i++)
        {
            if (fgets(line, sizeof(line), wordsfile) == 0)
                break;
            size_t len = strlen(line);
            line[len-1] = '\0';  // Zap newline
            strncpy(word_array[i], line, sizeof(word_array[i])-1);
            word_array[i][sizeof(word_array[i])-1] = '\0';
            printf("   %s\n", word_array[i]);
        }
        fclose(wordsfile);
    }
    return i;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
1

You scanf into word_array which is essentially of type char**, you want it to be the string at word_array[index], for a given running index.

Also note that feof is tricky in some cases, read here for more details - Why is “while ( !feof (file) )” always wrong?

Community
  • 1
  • 1
Leeor
  • 19,260
  • 5
  • 56
  • 87