私は現在、ファイルを読み取るプログラムで一意の単語を検索し、その単語がファイルに出現する回数をカウントするようにしようとしています。私が現在ユーザーに単語を尋ね、その単語が出現する回数をファイルで検索しているもの。ただし、ユーザーに個別の単語を要求するのではなく、プログラム自体でファイルを読み取る必要があります。
これは私が現在持っているものです:
#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;
}
私のプログラムに関する他の提案はありがたいですが、ユニークな単語(まだチェックされていない単語)のファイルを読み取る方法を理解するのに少し助けが必要です。