-2

我有一个文本文件,其模式如下:

1,2,3$
2,4,1,5,6$
3,1,2,9$
1,0,9,8,7,3$
3,8,2,9$
   .
   .
   .
   .

我有一个char new[]="3,1,2,9$"

我需要一个 C 函数来检查上面提到的文本文件是否包含char new[]="3,1,2,9$"

这是我的代码,但它不起作用:

int main(void)
{
    char new[100]="0,1268,1236$";
    FILE *my_file;
    char string[2000];
    int ind=0;
    int rest;
    int found = 0;

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

    while((string[ind++]=getchar())!=EOF)
    {
        if(string[ind]='$')
        {
            rest=strcmp(string,new);
            if(rest==0)
            {
                found =1;
                printf("found");
                ind=0;
            }
            strcpy(string, "");
        }
    }
    fclose(my_file);
}

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

4

1 回答 1

2

您正在打开文件。但是,你似乎根本没有从中读到任何东西。

似乎是从控制台读取的getchar(),或者您没有正确发布代码。

请使用fgetsfread从文件中读取数据,然后进行比较。

于 2013-03-08T07:23:27.477 回答