.txtファイルで特定の単語を検索したかったのです。
たとえば、ファイルには「Jon Miller、Andy Miller、Apu McDawn」が含まれています。このファイルで「Miller」が発生する頻度を検索したいと思います。次に、番号(num)「2」が表示されます。
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char const *argv[])
{
int num =0;
char word[100];
char *string;
FILE *in_file = fopen("test.txt", "r"); //reading the words
if (in_file == NULL)
{
printf("Dataerror\n"); //if word not found
exit(-1);
}
else {
scanf("%s", word);
printf("%s\n", word);
while(!feof(in_file))//search for the word
{
fscanf(in_file,"%s", string);
if(!strcmp(string , word))//if hit a word
num++;
}
printf( "%d Hit \n" ,num );
}
return 0;
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加