0

I'm trying to read a file (full of a word followed by a newline), to an array full of pointers to each string. Then print each word in the array, and count the number of words read. However it just prints no words and says 0 words imported.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define ARGS_REQUIRED 2
#define MAX_WORDS 50
#define MAX_WORD_LENGTH 1024

void read_file (char * argv[], char word_storage[]);
void usage (char * argv[]);

int main (int argc, char* argv[])

{
char word_storage[MAX_WORDS];

if (argc == ARGS_REQUIRED)
    {
        system("clear");
        read_file(&argv[1], word_storage);
    }
else 
    {
        usage(&argv[0]);
    }

return 0;

}

void usage (char * argv[])

{
     printf("Incorrect usage, try: ./program_name %s\n", argv[1]);
}

void read_file (char * argv[], char word_storage[])

{

FILE * file_name;
char *word[MAX_WORDS][MAX_WORD_LENGTH]; 
int word_count = 0, i = 0, j;

    if ((file_name = fopen(argv[0], "r")) == NULL)
        {
            printf("Cannot open file ... \n");
        }
    while (fscanf(file_name, "%s", *word[MAX_WORDS]) == 1)
        {
            for (j = 0; j < MAX_WORDS; j++)
                {
                   if (printf("%s\n", *word[j]) == 1)
                    {
                        word_count++;
                    }
                }

        }

fclose(file_name);        
printf("Imported words: %d\n", word_count);

}
11
  • To begin with, fscanf(file_name, "%s", *word) in this program is dangerous because *word (this is equivalent to word[0]) isn't initialized and its value is indeterminate. Commented Oct 31, 2015 at 15:40
  • Detail: Files typically do not have C strings (array of char ending with a '\0'). More often, that have lines of text (array of char ending with a '\n') Commented Oct 31, 2015 at 15:41
  • 1
    @chux It may not be true because &argv[1] is passed to read_file from main(). Commented Oct 31, 2015 at 15:59
  • 1
    MikeCAT suggested char word[...][...];, not char *word[...][...]; (no *) Commented Oct 31, 2015 at 16:27
  • 1
    Please stop changing the code. And append notes/code if needed. Commented Oct 31, 2015 at 16:29

1 Answer 1

1

You can modify your program as below. It worked for me. Please add error check for printf() function.

void read_file (char * argv[], char word_storage[])
{

FILE * file_name;
char word[MAX_WORDS][MAX_WORD_LENGTH];
int word_count = 0, i = 0, j;

    if ((file_name = fopen(argv[0], "r")) == NULL)
        {
            printf("Cannot open file ... \n");
        }
    while (fscanf(file_name, "%s", word) == 1)
        {
printf("%s\n",word);
word_count++;
/*
            for (j = 0; j < MAX_WORDS; j++)
                {
                    if (printf("%s\n", word[j]) == 1)
                    {
                        word_count++;
                    }
                }
*/
        }

fclose(file_name);
printf("Imported words: %d\n", word_count);

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.