C语言 从文件中读取单词并计算它们在文件中出现的程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15508828/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:47:00  来源:igfitidea点击:

Program to read words from a file and count their occurrence in the file

cfile

提问by undone98

I'm currently trying to make a program that will read a file find each unique word and count the number of times that word appears in the file. What I have currently ask the user for a word and searches the file for the number of times that word appears. However I need the program to read the file by itself instead of asking the user for an individual word.

我目前正在尝试制作一个程序来读取文件,找到每个唯一的单词并计算该单词在文件中出现的次数。我目前要求用户输入一个单词并在文件中搜索该单词出现的次数。但是,我需要程序自己读取文件,而不是要求用户输入单个单词。

This is what I have currently:

这是我目前所拥有的:

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

int main(int argc, char const *argv[])
{   
int num =0;
char word[2000];
char *string;

FILE *in_file = fopen("words.txt", "r");

if (in_file == NULL)
{
    printf("Error file missing\n");
    exit(-1);
}

scanf("%s",word);

printf("%s\n", word);

while(!feof(in_file))//this loop searches the for the current word
{
    fscanf(in_file,"%s",string);
    if(!strcmp(string,word))//if match found increment num
    num++;
}
printf("we found the word %s in the file %d times\n",word,num );
return 0;
}

I just need some help figuring out how to read the file for unique words (words it hasn't checked for yet) although any other suggestions for my program will be appreciated.

我只需要一些帮助来弄清楚如何读取文件中的唯一单词(尚未检查的单词),尽管对我的程序有任何其他建议将不胜感激。

采纳答案by md5

If you want to print every line contained in the file just once, you have to save the strings you have read in a given data structure. For example, a sorted array could do the trick. The code might look as follow:

如果您只想打印文件中包含的每一行,则必须将读取的字符串保存在给定的数据结构中。例如,排序数组可以解决问题。代码可能如下所示:

#include <stddef.h>

size_t numberOfLine = getNumberOfLine (file);
char **previousStrings = allocArray (numberOfLine, maxStringSize);
size_t i;

for (i = 0; i < numberOfLine; i++)
{
    char *currentString = readNextLine (file);

    if (!containString (previousStrings, currentString))
    {
        printString (currentString);
        insertString (previousStrings, currentString);
    }
}

You may use binary search to code the functions containStringand insertStringin an efficient way. See herefor further informations.

您可以使用二进制搜索代码的功能containString,并insertString以高效的方式。请参阅此处了解更多信息。

回答by anatolyg

You have to split your code into functions (subroutines).

您必须将代码拆分为函数(子例程)。

One function would read the file and record all words; the other would count the number of occurrences for each word.

一种功能是读取文件并记录所有单词;另一个将计算每个单词的出现次数。

int main(int argc, char const *argv[])
{
    char *words[2000];

    // Read the file; store all words in the list
    int number_of_words = ReadWords("words.txt", words, 2000);

    // Now count and print the number of occurrences for each word
    for (int i = 0; i < number_of_words; i++)
    {
        int n = CountOccurrences(words[i], "words.txt");
        printf("we found the word %s in the file %d times\n", words[i], n);
    }

    // Deallocate dynamically allocated memory
    Cleanup(words, number_of_words);
}

Note how the mainfunction is relatively short. All the details are in the functions ReadWordsand CountOccurrences.

请注意该main函数是如何相对较短的。所有细节都在函数ReadWordsCountOccurrences.

To implement reading all words from a file:

要实现从文件中读取所有单词:

int ReadWords(const char *filename, char *words[], int max_number_of_words)
{
    FILE *f = fopen(filename, "rt"); // checking for NULL is boring; i omit it
    int i;
    char temp[100]; // assuming the words cannot be too long

    for (i = 0; i < max_number_of_words; ++i)
    {
        // Read a word from the file
        if (fscanf(f, "%s", temp) != 1)
            break;
        // note: "!=1" checks for end-of-file; using feof for that is usually a bug

        // Allocate memory for the word, because temp is too temporary
        words[i] = strdup(temp);
    }
    fclose(f);

    // The result of this function is the number of words in the file
    return i;
}

回答by Chandra Mani

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

int main(int argc, char*argv[])
{   
int num =0;
char word[2000];
char string[30];

FILE *in_file = fopen(argv[1], "r");

if (in_file == NULL)
{
    printf("Error file missing\n");
    exit(-1);
}

scanf("%s",word);

printf("%s\n", word);

while(!feof(in_file))//this loop searches the for the current word
{
    fscanf(in_file,"%s",string);
    if(!strcmp(string,word))//if match found increment num
    num++;
}
printf("we found the word %s in the file %d times\n",word,num );
return 0;
}`

if any suggestion plz..most welcome

Blockquote