简体   繁体   English

如何在c的文本文件中重写该单词频率计数器程序?

[英]How to rewrite this program of words frequency counter in the text file in c?

Here we see the program which computes how frequent each word of text file is present. 在这里,我们看到了一个程序,该程序计算文本文件中每个单词出现的频率。 After some small corrections it becomes to work perfectly for sufficiently small files. 经过一些小的更正后,它对于足够小的文件将变得完美工作。 I wanted to use it for large text file, but get an error "Segmentation fault". 我想将其用于大型文本文件,但出现错误“分段错误”。 The reason is that there is an initialization of array 原因是存在数组的初始化

 char p[1000][512], 

which is so small for large text (if I understand correctly, it can save only 1000 words (which in general may coincide)). 这对于大文本来说太小了(如果我理解正确的话,它只能保存1000个单词(通常可能是一致的))。 If I try to enlarge the dimension of p, I also get this error (there cannot be arrays larger than 2000*2000 on my computer). 如果尝试扩大p的维数,也会出现此错误(计算机上的数组不能大于2000 * 2000)。

Could the code above be modified in order for opening large text files? 为了打开大型文本文件,可以修改上面的代码吗? If yes, how to do that? 如果是,该怎么做? Could You write the code which modifies it? 您可以编写修改它的代码吗?

Consider allocating your array on head using malloc . 考虑使用malloc在头上分配数组。

When you declare your array like char char p[1000][512] , it allocates 512 * 1000 (about 512 Kb) on stack. 当像char char p[1000][512]这样声明数组时,它将在堆栈上分配512 * 1000(约512 Kb)。 Stack size is insufficient for large files. 堆栈大小不足以容纳大文件。 When you allocate your memory using malloc, you ask operating system to give you some additional memory in heap. 使用malloc分配内存时,您要求操作系统在堆中提供一些额外的内存。

So, instead of your code you should do like 因此,除了您的代码外,您应该喜欢

typedef char * string_t;
string_t * stringsArray = malloc(sizeof(string_t) * NUM_STRINGS_TO_ALLOCATE);
for (size_t i = 0; i < STRINGS_COUNT; ++i)
   stringsArray[i] = malloc(sizeof(char) * NUM_CHARS_PER_STRING);

don't forget to free allocated memory after using it, like: 使用完内存后不要忘记free分配的内存,例如:

for (size_t i = 0; i < STRINGS_COUNT; ++i)
   free(stringsArray[i]);
free(stringsArray);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C程序来计算文本文件中的单词频率 - C Program to count the word frequency in a text file C程序对文本文件中的单词进行排序,并使用链接列表打印其频率,但未计算频率 - C program to sort words from text files and print their frequency using linked lists but frequency is not getting calculated 在c中的文本文件中找到单词出现频率与单词总数之比的问题 - Problem finding the ratio of frequency of a word to total no of words in a text file in c C程序未计算文本文件中的字母频率 - C Program is not counting letter frequency from text file C 中的程序计算文本文件中给定单词的频率 - Program in C to count the frequency of a given word in a text file 从C中的文件中查找单词的频率出现问题 - Trouble finding frequency of words from a file in C 如何实现一个C程序来显示文本文件中五个最长的单词? - How to implement a C program that display five longest words from a text file? C编程:一种计算文本文件中单词数量的程序? - C programming: a program that counts the number of words in a text file? 打印给定文本文件中出现频率最高的单词,无法按C中的频率排序 - Printing the most frequent occurring words in a given text file, unable to sort by frequency in C 如何在 C 中重写文本文件中的部分行 - How to rewrite part of a line in a text file in C
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM