计算文件C中的单词出现次数

时间:2015-06-12 17:03:48

标签: c string count

欢迎大家。我是Stackoverflow的新手,我在C中编码了一段时间。 我在编写一个计算文本文件中单词出现次数的程序时遇到了问题。我需要有一个输出,告诉我发生了多少次单词。这是源代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int new_words=0;
int nwords=0;

typedef struct element{
    char word[30];
    int how_many;
} element;

int is_word_new(element ** dictionary, char * string)
{
    for (int i =0; i<new_words; i++)
    {
        if (strcmp(string, dictionary[i]->word)==0)
            return 0;
    }
    return 1;
}
int which_word(element ** dictionary, char * string)
{
    for (int i =0; i<new_words; i++)
    {
        if (strcmp(string, dictionary[i]->word)==0)
            return i;
    }
    return 0;
}

int main()
{
    FILE * fp;
    char word[30];


    fp=fopen("input.txt", "r");
    if (fp==NULL)
    {
        printf("FILE ERROR");
        return 0;
    }


    while(!feof(fp))
    {
        fscanf(fp, "%s",word);
        nwords++;
    }
    nwords--;
    rewind(fp);

    struct element * dictionary = (element*)malloc(sizeof(element)*nwords);

    for (int i =0; i<nwords; i ++)
    {
        fscanf(fp, "%s", word);

        if( is_word_new(&dictionary, word) )
        {
            strcpy(dictionary[new_words].word, word);
            //dictionary[new_words].word= word;
            dictionary[new_words].how_many=1;
            new_words++;
        }
        else
            dictionary[which_word(&dictionary, word)].how_many++;
        word[0]='\0';
    }

    printf("\n\nFinal dictionary\n with %d words", new_words);
    for (int i =0; i<new_words; i++)
    {
        printf("%s %d \n", dictionary[i].word, dictionary[i].how_many);     
    }

    free(dictionary);
    fclose(fp);
    return 0;
}

这个想法是我首先计算文本中有多少单词(以某种方式总是比实际上大一个)。函数is_word_new检查新读取的单词是否已经在字典中。 which_word()告诉我找到哪个单词

但是我运行此程序会出现分段错误。 当我使用被注释为// dictionary[i].word=word的行时,程序就像在字典中只有“单词”一样。

请给我提示我在哪里做错了

1 个答案:

答案 0 :(得分:0)

必读问题:Why is “while ( !feof (file) )” always wrong?感谢Jonathan Leffler的评论。

请在下面的代码中查看我的评论。当你的话出现一次时,我帮你了。我正在为你完成剩下的工作,以便我们分享乐趣,但你当然可以问。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int new_words = 0;
int nwords = 0;

typedef struct element {
    char word[30];
    int how_many;
} element;

// no need to pass double pointer
int is_word_new(element* dictionary, char * string) {
    int i;
    for (i = 0; i < new_words; i++) {
        printf("|%s|, |%s|\n", string, dictionary[i].word);
        if (strcmp(string, dictionary[i].word) == 0)
            return 0;
        printf("i=%d\n",i);
    }
    return 1;
}

int which_word(element ** dictionary, char * string) {
    int i;
    for (i = 0; i < new_words; i++) {
        if (strcmp(string, dictionary[i]->word) == 0)
            return i;
    }
    return 0;
}

int main() {
    FILE * fp;
    char word[30];


    fp = fopen("test.txt", "r");
    if (fp == NULL) {
        printf("FILE ERROR");
        return 0;
    }

    printf("file read\n");

    int read_counter;
    while (!feof(fp)) {
        read_counter = fscanf(fp, "%s", word);
        // increment only if we really read something
        if(read_counter >= 0)
                nwords++;
    }
    // this is wrong, remove it
    //nwords--;
    rewind(fp);

    printf("nwords = %d\n", nwords);
    // do not cast what malloc returns. Also struct is not needed.
    element * dictionary = malloc(sizeof (element) * nwords);

    int i;
    for (i = 0; i < nwords; i++) {
        fscanf(fp, "%s", word);
        printf("read |%s|\n", word);
        if (is_word_new(dictionary, word)) {
            strcpy(dictionary[new_words].word, word);
            //dictionary[new_words].word= word;                     
            dictionary[new_words].how_many = 1;
            new_words++;
        } else {
            printf("bhka\n");
            dictionary[which_word(&dictionary, word)].how_many++;
        }
        //word[0] = '\0';
    }

    printf("\n\nFinal dictionary\n with %d words", new_words);
    for (i = 0; i < new_words; i++) {
        printf("%s %d \n", dictionary[i].word, dictionary[i].how_many);
    }

    free(dictionary);
    fclose(fp);
    return 0;
}

这是我使用的test.txt:

sam klouvi george dit epfl
ok
end