-2

The snippets below are from my program that gets words, then prints them with the number of occurences.

It works almost fine except for it "forgots" that particular entry has been saved before and does NOT increment counter associated with it.

typedef struct {
    char *word;
    int occ;
}
words;
words *data=NULL;

int main(int argc, char **argv)
{
    char *word;
    words *temp;
    int c,i,num;
    words *ptr = NULL;

    num=0;

    while(1)
    {
        c=fgetc(infile);
        if(c==EOF) break;
        if(!isalpha(c)) continue;
        else ungetc(c,infile);
        word=getword(infile);

        if(findword(word))
        {

            if(!(temp=realloc(data,sizeof(words)*(num+1))))
            { /* error handling */ }
            else
                data=temp;



        }
        else
           free(word);
    }

    /* sort procedure here, irrelevant for the purpose of topic */
    for(i=0;i<num;i++)
    {
        /*printf*/
    }

    return 0;

}

What's wrong with that code?

Thanks in advance!

4

2 回答 2

0

根本问题是你的“findword”功能实际上并没有找到一个词。它只是查看列表中的一项。它需要循环。

于 2012-09-18T20:23:35.710 回答
0
if ((strcmp(word, ptr->word)) == 0) {
  //Do something to store the count
} 
else {
  return ptr;
}
于 2012-09-18T20:09:54.140 回答