2

私は現在、ファイルを読み取るプログラムで一意の単語を検索し、その単語がファイルに出現する回数をカウントするようにしようとしています。私が現在ユーザーに単語を尋ね、その単語が出現する回数をファイルで検索しているもの。ただし、ユーザーに個別の単語を要求するのではなく、プログラム自体でファイルを読み取る必要があります。

これは私が現在持っているものです:

#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;
}

私のプログラムに関する他の提案はありがたいですが、ユニークな単語(まだチェックされていない単語)のファイルを読み取る方法を理解するのに少し助けが必要です。

4

3 に答える 3

1

ファイルに含まれるすべての行を1回だけ印刷する場合は、読み取った文字列を特定のデータ構造に保存する必要があります。たとえば、ソートされた配列でうまくいく可能性があります。コードは次のようになります。

#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);
    }
}

二分探索を使用して、関数containStringinsertString効率的にコーディングできます。詳細については、こちらをご覧ください。

于 2013-03-19T19:40:16.277 に答える
1

コードを関数(サブルーチン)に分割する必要があります。

1つの関数がファイルを読み取り、すべての単語を記録します。もう1つは、各単語の出現回数をカウントします。

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);
}

main関数が比較的短いことに注意してください。詳細はすべて関数ReadWordsとにありCountOccurrencesます。

ファイルからすべての単語を読み取ることを実装するには:

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;
}
于 2013-03-19T21:37:46.573 に答える
0
`#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
于 2016-08-24T17:36:42.747 に答える