c - 用c记录文本文件中的每个单词

标签 c string

我正在尝试构建一个函数来检查该单词是否在单词列表中,如果是,它将增加该单词频率的相应计数器。否则,它将创建一个副本 单词并将其附加到列表中。然后将相应的频率计数器设置为1。 我没有遇到编译器错误,但是当我尝试打印任何单词的频率时,我得到了 200 万个数字,我不知道为什么。 我得到了一个无法修改的主文件:

#include <stdlib.h>
#include <string.h>
#define MAX_WORDS 300
#define LINE_LEN 80

void increment_word_freq(char *freq_words[MAX_WORDS], int *frequency, int *n, char *word);

int main(){
    char delim[] = " ,.!-;\"\n";
    char filename[] = "cookbook.txt";
    char line[LINE_LEN];
    char *word;
    char *freq_words[MAX_WORDS]; // a list of frequent words
    int frequency[MAX_WORDS]; // frequency of the words
    int n = 0; // number of words in the list
    int min_occr;
    FILE *fp;
    fp = fopen(filename, "r");
    if(!fp){
        printf("Could not open file %s\n", filename);
        exit(1);
    }

    // read one line at a time
    while(fgets(line, LINE_LEN, fp)){
        // get the words from the line
        word = strtok(line, delim);
        while(word != NULL) {
            // convert the word to lowercase
            int i;
            for(i = 0; i < strlen(word); i++)
                word[i] = tolower(word[i]);
            increment_word_freq(freq_words, frequency, &n, word);
            word = strtok(NULL,delim);
         }
    }
}

这是我尝试使用的功能:

void increment_word_freq(char *freq_words[MAX_WORDS], int *frequency, int *n, char *word){

for(int i=0; i<MAX_WORDS; i++){
    if(freq_words[i] == word){
        frequency[i]++;
        break;
    }
    else if(i=MAX_WORDS-1){
        frequency[i]= *word;
        *n++;
    }
}
}

就像我之前说的,没有编译器错误,但尝试打印任何单词的频率将给出 200 万个数字,我不知道为什么。 非常感谢任何和所有的帮助和建议!

最佳答案

freq_words[i] == word 仅将指针 freq_words[i] 与指针 word 进行比较。您必须比较指针引用的字符串。将代码更改为 strcmp(freq_words[i], word) == 0。除此之外,您还必须分配动态内存来存储字符串。使用strcpy将字符串复制到动态内存中。你必须这样做,因为 word 是一个指向 line 中某处 char 的指针,但是 line 将是如果您读取文件的下一行,则会被覆盖。像这样调整您的代码:

#include <string.h> // strcmp, strcpy

void increment_word_freq( char *freq_words[MAX_WORDS], int *frequency, int *n, char *word)
{
    for ( int i=0; i < *n; i++) // for all current members of freq_words
    {
        if ( strcmp( freq_words[i], word ) == 0 ) // test if word is member of freq_words
        {
            frequency[i]++; // increment count
            return;         // finished, because word was found 
        }
    }

    // word was not found in freq_words => add new word to freq_words 
    if ( *n < MAX_WORDS-1 ) // test if there is one more place in freq_words
    {
       freq_words[*n] = malloc( strlen(word) + 1 );   // allocate dynamic memory for new meber of freq_words
       strcpy( freq_words[*n], word );                // copy word to freq_words[*n]
       frequency[*n] = 1;                             // int frequency[*n] with 1
       (*n)++;                                        // increment count of members of freq_words
    }
}

请注意,您必须在 main 末尾释放分配的内存,否则会出现内存泄漏。

for ( int i=0; i < *n; i++)
{
    free( freq_words[i] );
}

关于c - 用c记录文本文件中的每个单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35364763/

相关文章:

C 字符串指针初始化自身然后取消初始化自身?

c - C中位的右循环

c - 当进程 fork 时,共享库 .so 是否仍在地址空间中?构造函数会再次执行吗?

javascript - 计算jquery自动完成字段中多个选择的数量

c - ANSI C 中的字符串处理(内存分配)

c - 从文件读取内容时出现段错误

c++ - 如何使用 SIMD 比较两个 char vector 并将结果存储为 float ?

java - 最大重复字符数和数量

c - 将字符数组用作整数

c - 在没有 errno 的情况下实现 strtol() 是否更好?