C - 如何计算文件中每个单词的出现次数并删除重复单词

标签 c struct fgets

我目前在大学的任务是从文件中读取单词并计算该文件中每个单词的出现次数,将结果打印到控制台/新文件。

我已经成功地计算了单词出现的次数,但是,我很难删除其余的值。

例如我想删除额外出现的 C,但保留其计数。

C:2 他们:2 是:1 不:1 他们:2 写:1 在 : 1 C:2

这是我目前拥有的代码...

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

#define SIZE 256

struct words
{
    char *word;
    unsigned int count;
};

int count_words()
{
    // Allocate memory for first word to compare + words struct
    char *key_word = (char*)malloc(sizeof(char) * SIZE);
    struct words *w = (struct words*)malloc(sizeof(struct words) * SIZE);
    // Create variable to read words from file
    FILE *word_list = fopen("single_words_test.txt", "r");
    // Variable to store total word count
    int total_words = 0;

    // Read in words from file line by line
    while (fgets(key_word, SIZE, word_list) != NULL)
    {
        // Remove the newline character
        key_word[strlen(key_word) - 1] = '\0';
        // Initialize members of words structure
        w->word = key_word;
        w->count = 0;
        // Allocate memory for current word being compared to key_word
        char *current_word = (char*)malloc(sizeof(char) * SIZE);
        // Create variable to read second list of words from file
        FILE *word_list2 = fopen("single_words_test.txt", "r");

        while (fgets(current_word, SIZE, word_list2) != NULL)
        {
            // Remove newline character
            current_word[strlen(current_word) - 1] = '\0';
            // If currrent read word matches keyword, increase its count
            if (strcmp(key_word, current_word) == 0)
            {
                w->count++;
            }
        }
        // Free the allocated memory
        free(current_word);
        fclose(word_list2);

        total_words++;

        printf("%s : %d\n", w->word, w->count);
    }

    free(w);
    free(key_word);
    fclose(word_list);

    return total_words;
}

int main(int argc, char **argv)
{

    printf("\n\n\n%d\n\n\n", count_words());

    return 0;

}

我知道代码很困惑,但我已经坚持了一段时间,并且不确定如何将其实现到我当前的解决方案中。

此外,我知道这可以通过创建链接列表来完成,但我想避免该解决方案并使其与当前解决方案类似。

谢谢您,对于问题的含糊之处表示歉意

编辑:这不是代码请求。我只是想要一些关于我可以使用什么的一般指导

最佳答案

检查您在列表中是否找到了比您要查找的key_word更早的current_word。您可以在文件指针上使用 ftell(...) 来完成此操作。

这是一个例子;

int count_words()
{
    // Allocate memory for first word to compare + words struct
    char *key_word = (char*)malloc(sizeof(char) * SIZE);
    struct words *w = (struct words*)malloc(sizeof(struct words) * SIZE);
    // Create variable to read words from file
    FILE *word_list = fopen("single_words_test.txt", "r");
    // Variable to store total word count
    int total_words = 0;
    long word_list_pos, word_list2_pos;


    // Read in words from file line by line
    while (fgets(key_word, SIZE, word_list) != NULL)
    {
        // Remove the newline character
        key_word[strlen(key_word) - 1] = '\0';
        // Initialize members of words structure
        w->word = strdup(key_word);
        w->count = 0;
        word_list_pos = ftell(word_list);

        // Allocate memory for current word being compared to key_word
        char *current_word = (char*)malloc(sizeof(char) * SIZE);
        // Create variable to read second list of words from file
        FILE *word_list2 = fopen("single_words_test.txt", "r");

        while (fgets(current_word, SIZE, word_list2) != NULL)
        {
            // Remove newline character
            current_word[strlen(current_word) - 1] = '\0';
            // If currrent read word matches keyword, increase its count
            if (strcmp(key_word, current_word) == 0)
            {
                word_list2_pos = ftell(word_list2);                
                if (word_list2_pos < word_list_pos)
                    break;

                w->count++;
            }
        }
        total_words++;

        if (word_list2_pos >= word_list_pos)
            printf("%s : %d\n", w->word, w->count);

        // Free the allocated memory
        free(w->word);
        free(current_word);
        fclose(word_list2);

    }

    free(w);
    free(key_word);
    fclose(word_list);

    return total_words;
}

关于C - 如何计算文件中每个单词的出现次数并删除重复单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48795512/

相关文章:

c - 完成后将openmp线程返回到单线程

object - 在 C# 中从 Struct 调用属性

c - 创建房间的结构,然后将每个房间写入各自的唯一文件

C - 带有嵌套 fgets 循环的 fork 和管道

c - 使用 C 语言编写的 Unix 尾部程序,使用已实现的函数

c - Entab 程序意外错误(读访问冲突)

c - fwrite() 没有写入指定次数的数据

c++ - 是否存在两次包含相同 header 实际上有帮助的情况?

c - 有没有更短的方法来初始化结构数组?

c - if语句与C中的字符串比较