我正在尝试将某些单词保存到指向数组(**valid_words)的指针中,但我遇到了一个我不明白的问题。下面是我尝试使用的功能,最终结果是根据 iline 和 dline 相等时的条件,valid_words 包含所有有效单词。
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");
}
}
我得到的当前输出是
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
我期待在零:总是输出“英里”,但这并没有发生。调用该函数后,打印 valid_words[i] 不会打印任何内容。如果有人可以帮助我,我将非常感激,并且非常愿意接受任何批评。你可以在这里找到完整的实现http://pastebin.com/TjxLRVaC 谢谢。