1

I'm trying to save certain words into a pointer to an array (**valid_words), but I am running into an issue I do not understand. Below is the function I am trying to utilize, end result is to have valid_words contains all the valid words according to the condition when iline and dline are equal.

void CheckFile(char *input_file, char *dictionary_file, char *output_file, char **valid_words)
{
    /* Open files as read or append */
    FILE *input, *dictionary, *output;
    input = fopen(input_file, "r");
    dictionary = fopen(dictionary_file, "r");
    output = fopen(output_file,"a");
    int word_count = 0;

    /* Read from files */
    if (input != NULL && dictionary!= NULL)
    {   
        char *iline = malloc(MAXSIZE*sizeof(char)); 
        char *dline = malloc(MAXSIZE*sizeof(char));

        while (fgets (iline, sizeof iline, input) != NULL)
        {   
            while (fgets (dline, sizeof dline, dictionary) != NULL)
            {   
                trimwhitespace(iline);
                trimwhitespace(dline);
                if (strcasecmp(iline, dline) == 0 ) 
                {   
                    fprintf(output, "%s\n",iline);
                    valid_words[word_count] = iline;
                    printf("Word Count: %d\n", word_count);
                    printf("At Zero: %s\n", valid_words[0]);
                    printf("Valid Word: %s\n", valid_words[word_count]);
                    printf("Actual Word: %s\n", iline);
                    word_count++;
                }   

            }   
            rewind(dictionary);
        }   

        fclose(input);
        fclose(dictionary);
        fclose(output);
        free(iline);
        free(dline);
    }   
    else
    {   
        printf("An error has occured\n");
    }   
}

The current output I am getting is

Word Count: 0
At Zero: miles
Valid Word: miles
Actual Word: miles
Word Count: 1
At Zero: limes
Valid Word: limes
Actual Word: limes
Word Count: 2
At Zero: smile
Valid Word: smile
Actual Word: smile
Word Count: 3
At Zero: slime
Valid Word: slime
Actual Word: slime

I am expecting At Zero: to always output "miles", but this is not happening. After the function has been called printing valid_words[i] results in nothing being printed out. I will really appreciate it if someone can help me out, and am very open to any criticism. You can find the full implementation here at http://pastebin.com/TjxLRVaC Thank you.

8
  • This ain't looking so hot: sizeof iline. Did you want that to be the size of a pointer ? Same goes for dline Commented Jan 10, 2013 at 0:44
  • @WhozCraig I want iline and dline to point to a string with six characters, right now MAXSIZE is defined as six. So if my thought process is correct I should have reserved six bytes for iline and dline. Commented Jan 10, 2013 at 1:17
  • You probably did, but sizeof iline is the size of a pointer; not the size of what it points to. Likewise with sizeof dline. Offhand, what is the format of the dictionary text file, is it just whitespace separated words (whitespace being spaces, tabs, newlines, etc)? I ask because I'm not convinced your filtering process is going to do what you think it should. Commented Jan 10, 2013 at 1:21
  • Hmm.. okay I will look further into the sizeof pointer, you are referring to char iline = malloc(MAXSIZEsizeof(char)); right? Also both my input file and dictionary file have one word per line followed by a newline. Commented Jan 10, 2013 at 1:27
  • Ok. that is somewhat important (the info about the file format). And yes I was referring to the dynamic allocation lines. There are a host of other issues I'm in the process of typing up. It may take some time, but the link you provided to your full source helps a lot. You have the right idea on how to solve the problem, just going about it all wrong with a likely misunderstand of things like pointers and variable sizing. Let me work on it awhile. Hope you have some time. Commented Jan 10, 2013 at 1:30

1 Answer 1

1

The answer to why it doesn't work is -

valid_words[word_count] = iline;

You should use either strcpy after ensuring that the element in valid_words can hold up to MAXSIZE characters or use strncat (NB: not strncpy) after ensuring that the first (really zeroth) character in the element in valid_words that you are inserting is '\0'.

Some observations:

Assuming MAXSIZE is a #defined constant, this is better -

char iline[MAXSIZE]; 
char dline[MAXSIZE;

than the malloc calls that you have (by the way, sizeof(char) is one by definition.)

Likewise, you are better off changing the last parameter of CheckFile to char valid_words[][MAXSIZE] (and thus changing the arguments in all the calls to CheckFile).

Hope this helps.

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

2 Comments

I think you meant to say "then the malloc() and free() calls that you have can be eliminated.
I meant to say that declaring them as arrays of char is better than the malloc calls that the [OP] has.

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.