1

我正在编写一个 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

4 回答 4

2

假设这是一个家庭作业问题,是否保证句子都在不同的行上并以“/”结尾?

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

于 2013-03-09T05:06:31.220 回答
2

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

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

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

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

if (got == '\n') continue;
于 2013-03-09T05:12:17.450 回答
1

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

If you want to compare the array A[10] with B[10], write a function that takes char* A, char* B and its int size as input, then compare each element from A with B through a loop that runs size times.

于 2013-03-09T08:07:09.673 回答
0

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

你真的需要使用一个string或一个ary数组吗?考虑一个对 a和 acompare进行操作的函数,而不是对 two进行操作。FILE *char *char *

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

初学者倾向于使算法过于复杂。-耸耸肩-

于 2013-03-09T05:30:37.800 回答