2013-03-09 53 views
1

我正在寫一個C程序來讀取文件並檢查裏面是否有給定的句子。如果給定句子存在於文件中,該函數必須分別返回「找到」或「未找到」。句子被一個/符號分開。C程序爲了在文件中找到給定的句子

Example of file: 
1,2,3,4/ 
car, house, hotel/ 
2,age,12/ 
1,2/ 
1,2,3,5/ 
house, car/ 

Example of word to look for: 
1,2/ 

我的想法是每次冒了一句,從該文件,並把它放在一個陣列(稱爲元),檢查陣列(元)等於陣列(稱爲句子)包含給定我正在查找的句子,並將該數組(ary)重新用於文件中的下一句。

我寫這段代碼:

#include <stdio.h> 

void main() 
{ 
    char *sentence; 
    FILE *my_file; 
    char *ary; 
    int size = 500; 
    int got; 
    int ind=0; 
    int rest; 
    int found=0; 

    sentence="1,2"; 


    my_file=fopen("File.txt", "r"); 

    if(my_file==NULL) 
    { 
     printf("I couldn't open the file\n"); 
    } 
    else 
    { 
     ary = (char*)malloc(500*sizeof(char)); 
     while((got=fgetc(my_file))!=EOF) 
     { 
      if(got!='/') 
      { 
       ary[ind++]=(char)got; 
      } 
      else 
      { 
       ary[ind++]='\0'; 
       rest = compare(sentence,ary); 
       if(rest==0) 
       { 
        found =1; 
        printf("found\n"); 
        return; 
       } 
       ind=0; 
       free(ary); 
       ary = (char*)calloc(500, sizeof(char)); 
      } 
     } 
     if(found==0) 
     { 
      printf("not found\n"); 
     } 
     fclose(my_file); 
    } 
} 




int compare(char str1[], char str2[]) 
{ 
    int i = 0; 
    int risp; 
    if(str1>str2 || str1<str2) 
    { 
     risp=-1; 
    } 
    if(str1==str2) 
    { 
     while(str1[i++]!='\0') 
     { 
      if(str1[i]!=str2[i]) risp=1; 
     } 
    } 

    return risp; 
} 

它編譯,但不能正常工作,我不找出原因。

我試過另一種解決方案,更簡單,但不起作用。

void main() 
{ 
    char sentence[]="1,2"; 
    FILE *my_file; 
    char string[2000]; 
    int ind=0; 
    int rest; 
    int trovato = 0; 
    int got; 

    my_file=fopen("File.txt", "r"); 
    if(my_file==NULL) 
      printf("I couldn't open the file\n"); 
    else 
    { 
     string[0]='\0'; 
     while((got=fgetc(my_file))!=EOF) 
     { 
      if(got!='/') 
      { 
       string[ind++]=(char)got; 
      } 
      else 
      { 
       string[ind++]='\0'; 

       rest = compare(sentence, string); 
       if(rest==0) 
       { 
        found =1; 
        printf("found\n"); 
        return; 
       } 
       ind=0; 

       //delete the array 
       int x=0; 
       while(string[x]!='\0') 
       { 
        string[x]='\0'; 
        x++; 
       } 

      } 
     } 
     if(found==0) printf("not found\n"); 


     fclose(my_file); 
    } 
} 

有人能指出我的錯誤還是讓我知道更好的解決方案? 謝謝。

+3

「它不能正常工作」是什麼意思? 「不能正常工作」和「不能工作」除非你告訴我們他們的意思,否則沒有任何意義。我們無法從我們所在的位置看到您的屏幕,並且我無法從我現在的位置完全閱讀您的想法。請具體說明一下 - 您張貼了大量代碼以顯示您嘗試過的內容,這很好,但是如果您希望我們提供幫助,則需要告訴我們該代碼存在什麼問題。 :-) – 2013-03-09 05:07:38

回答

2

關於第一個代碼,比較功能是錯誤的。

這些檢查沒有任何意義,你可以改用STRCMP(或不比較指針):

if(str1>str2 || str1<str2) 
    { 
    risp=-1; 
    } 
    if(str1==str2) 

其次,要附加新行後/到新的句子,因此從未進行比較等於。這while循環的開頭添加:

if (got == '\n') continue; 
+0

謝謝。我解決了它。 – Alex 2013-03-09 20:12:39

2

假設這是一個作業問題,這些句子是否保證在單獨的行上並以'/'結尾?

如果是這種情況,應該使用getline函數,然後使用strcmp。

0

當然,compare因爲它比較指針,而不是字符串不工作(除了在指針是相等的情況下,這是一個情況下,你可以保證字符串相等,而不用測試每個字符)。地址在確定位於該地址的內容的平等性方面有什麼意義?沒有。

你真的需要使用string還是ary數組?考慮在FILE *char *上運行的compare函數,而不是兩個char *

有兩個參數:FILE *filechar *sentence。就像在任何比較功能中一樣,從size_t offset = 0;開始。每次獲得角色(從fgetc(file))時,請將其與sentence[offset]進行比較。如果匹配,則增加offset並重復。當sentence[offset] == '\0'時,您已達到句子結尾,沒有任何不匹配。這不是說你找到了一個句子嗎?如果不匹配,則將不匹配的字符放回(使用ungetc),並返回對應於「不匹配」的值。

初學者往往過於複雜的算法。-shrugs-

1

我不完全確定你爲什麼認爲比較函數應該起作用。如果你想比較兩個數組,你不會比較它們的起始地址。另外,在將函數集成到程序之前,請先用其他一些數據進行測試。

如果要陣列A [10]用B [10]比較,寫一個函數,採用char* Achar* B及其作爲輸入int size,那麼每個元件從A與B至運行size次的循環比較。

相關問題