我正在尝试构建一个函数来检查该单词是否在单词列表中,如果是,它将增加该单词频率的相应计数器。否则,它将创建一个副本 单词并将其附加到列表中。然后将相应的频率计数器设置为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/