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.

Miles
  • 189
  • 2
  • 13
  • This ain't looking so hot: `sizeof iline`. Did you want that to be the size of a *pointer* ? Same goes for `dline` – WhozCraig Jan 10 '13 at 00: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. – Miles Jan 10 '13 at 01: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. – WhozCraig Jan 10 '13 at 01:21
  • Hmm.. okay I will look further into the sizeof pointer, you are referring to char *iline = malloc(MAXSIZE*sizeof(char)); right? Also both my input file and dictionary file have one word per line followed by a newline. – Miles Jan 10 '13 at 01: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. – WhozCraig Jan 10 '13 at 01:30
  • Yes I do have time thank you. Its things like this that help me push forward :) – Miles Jan 10 '13 at 01:40
  • Barely had time to spend on this, but hopefully this will get you some ideas. I [posted this on ideone](http://ideone.com/MMUewU) so you can work with it. Might not be exactly what you're looking for, but should at least give you some thoughts. There are a couple of things to note in the code, including a few TODO's. Gotta head out node, but if I get a chance I'll come back to this. Thanks. – WhozCraig Jan 10 '13 at 03:29
  • Hey WhozCraig, just wanted to say thank you for posting your code. I've taken a look at it, and learned many concepts. I now have what I wanted working. I appreciate your efforts, kudos to you. – Miles Jan 13 '13 at 17:31

1 Answers1

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.

Happy Green Kid Naps
  • 1,611
  • 11
  • 18