c 操作檔案:實現關鍵詞搜尋

來源:互聯網
上載者:User

實現對以下英文文本的關鍵詞尋找。
支援如下形式調用: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,每行的數目n11     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.\n");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("\n請輸入關鍵字 :");//輸入關鍵字43     scanf("%s",str);44     for(i=0;i<MyCount;i++,n++)45     {46         flag=true;47         48         if(word[i]=='\n')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("\n關鍵字  %s  在文本裡面 %d,第 %d 個 字串.\n",str,m,++n);61             break;62         }    63     }64     getch();65     66 }

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.