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.
sizeof iline. Did you want that to be the size of a pointer ? Same goes fordlinesizeof ilineis the size of a pointer; not the size of what it points to. Likewise withsizeof 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.