2013-03-09 24 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次的循环比较。

相关问题