c操作ファイル:キーワード検索を実現する

9368 ワード

以下の英語テキストのキーワード検索を実現します.次の形式の呼び出しをサポートします:search.exe input.txt keyword出力:キーワードが初めて現れる位置ワイルドカードを実現すると加点が検索される(ドキュメントで説明が実現した場合)
 1 #include "stdio.h"

 2 #include "string.h"

 3 #include "stdlib.h"

 4 #include "conio.h"

 5 

 6 void main(int argc,char **argv)

 7 {

 8     int MyCount;        //      

 9     int i=0,j;

10     int m=0,n=0;      //  m,     n

11     FILE *fp;

12     char word[5000];   //        

13     

14     char str[20];     //   

15     bool flag;         //    

16 

17     char *Keyword;

18     char *input;

19     if(argc<3){

20         input="input.txt";

21         Keyword="1234";

22         

23     }else{

24         input=*(argv+1);

25         Keyword=*(argv+2);

26     }

27 

28     if((fp=fopen("input.txt","r"))==NULL)

29     {

30         printf("cannot open file.
"); 31 exit(0); 32 } 33 while(!feof(fp)) // 34 { 35 word[i]=fgetc(fp); 36 putchar(word[i]); 37 i++; 38 39 } 40 MyCount=i; 41 42 printf("
:
");// 43 scanf("%s",str); 44 for(i=0;i<MyCount;i++,n++) 45 { 46 flag=true; 47 48 if(word[i]=='
') 49 { 50 m++; 51 n=0; 52 } 53 for(j=0;str[j]!='\0';j++)// 54 if(str[j]!=word[i+j]) 55 { flag=false; 56 break; 57 } 58 if(flag==true) 59 { 60 printf("
%s %d, %d .
",str,m,++n); 61 break; 62 } 63 } 64 getch(); 65 66 }