C程序,以便在文件中查找给定的句子

时间:2013-03-09 04:47:17

标签: 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)中,检查数组(ary)是否等于包含给定句子的数组(称为句子)我正在寻找,并将该数组(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);
    }
}

有人可以指出我的错误或让我知道更好的解决方案吗? 谢谢。

4 个答案:

答案 0 :(得分:2)

假设这是一个家庭作业问题,句子是否保证在单独的行上并以'/'结尾?

如果是这种情况,则应使用getline函数,然后使用strcmp。

答案 1 :(得分:2)

关于第一个代码,比较功能是错误的。

这些检查没有意义,你可以使用strcmp(或者不比较指针):

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

其次,您将/之后的换行符附加到新句子,因此它永远不会比较相等。将其添加到while循环的开头:

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

答案 2 :(得分:1)

我不确定为什么你认为比较功能应该起作用。如果要比较两个阵列,则不要比较它们的起始地址。此外,在将函数集成到程序中之前,首先使用其他一些数据进行测试。

如果要将数组A [10]与B [10]进行比较,请编写一个以char* Achar* B及其int size为输入的函数,然后比较每个元素从A到B到一个运行size次的循环。

答案 3 :(得分:0)

当然,compare不起作用,因为它比较指针而不是字符串(指针相等的情况除外,这是一种可以保证指针的情况)字符串相等而不测试每个char)。地址在确定位于该地址的内容的相等性时有什么意义?无。

您真的需要使用stringary数组吗?考虑一个compare函数,该函数在FILE *char *上运行,而不是两个char *

接受两个论点:FILE *filechar *sentence。从size_t offset = 0;开始,就像在任何比较函数中一样。每次获得角色(来自fgetc(file))时,请将其与sentence[offset]进行比较。如果匹配,请递增offset并重复。当sentence[offset] == '\0'时,您已达到句末,没有任何不匹配。这不是说你发现了一个句子吗?如果它不匹配,请将不匹配的字符放回(使用ungetc)并返回与&#34;不匹配&#34;对应的值。

初学者倾向于过度复杂的算法。 -shrugs -