读取文本文件并搜索文本

时间:2021-05-20 07:40:37

标签: c

阅读文本文件中的材料后,如果通过搜索有对应的名字,打印出名字和钱。但是,我只能读取文本文件的第一行。如果我想让它读取另一行并找到相应的名称,我该怎么办?

我的文本文件有 15 行。

Jane 50
Bruno 100
Kim 200
Young 150
Will 250
Jane 50
Bruno 100
Kim 200
Young 150
Will 250
Jane 50
Bruno 100
Kim 335
Young 455
Will 555
#include <stdio.h>
#include <string.h>
#include <stdlib.h>



typedef struct user{
    char name [10];
    int money;
} userinformation;



int main()
{
    userinformation myvar[100];
    int x=0;
    char find_name[100];
    int i = 0;
    int idx = 0;
    char buffer[1001],*token;
    char* ptr;
    userinformation* userinfo;
    int cents_50=0;
    int cents_20=0;
    int cents_10=0;
    int cents_5=0;
    
    FILE * srcFile = fopen("coins.txt", "r");
    
    
    if(srcFile == NULL)
    {
        printf("fail to open file");
        return -1;
    }
    
    //file read from coins.txt
    while (!feof(srcFile))
    {
        i =0;
        fgets(buffer, 1001, srcFile);
        token = strtok(buffer, " ");
        while (token != NULL)
        {
            if(i == 0)
            {
                strcpy(myvar[idx].name, token);;
            }
            else if (i == 1)
            {
                myvar[idx].money = atoi(token);
            }
            i++;
            token = strtok(NULL, " ");
        }
        idx++;
    }
    
    
    for(int i = 0; i <idx; i++)
    {
        printf("%s %d\n", myvar[i].name, myvar[i].money);
    }
    fclose(srcFile);
    
    
    
    while(x != 2)
    {
        printf("1.Enter name\n");
        printf("2.Exit\n");
        scanf("%d%*c", &x);
        
        if(x ==1)
        {
            printf("Name: ");
            scanf("%s%*c", find_name);
            for(i=0; i<idx; i++)
            {
                ptr = strstr(myvar->name, find_name);
            }
            if(ptr != NULL)
            {   
                printf("Customer: \n");
                printf("%s %d\n", myvar->name, myvar->money);
                printf("Change: \n");
                //calculate 50cents;
                if(myvar->money>50) 
                {
                     myvar->money/50;
                     cents_50++;
                     printf("50 cents : %d \n", cents_50);
                }
                //calculate 20cents;
                if((myvar->money/50)<50 && myvar->money >=20)
                {
                    if((myvar->money%50/20)>=1)
                    {
                        (myvar->money%50/20);
                        cents_20++;
                        printf("20 cents : %d \n", cents_20);
                    }
                }
                //calculate 10cents;
                if((myvar->money%50%20/10)<20 && myvar->money >=10)
                {
                    if((myvar->money%50%20/10)>=1)
                    {
                        (myvar->money%50%20/10);
                        cents_10++;
                        printf("10 cents : %d \n", cents_10);
                    }
                }
                if((myvar->money%50%20%10/5)<10 && myvar->money >=5)
                {
                    if((myvar->money%50%20%10/5)>=1)
                    {
                        (myvar->money%50%20%10/5);
                        cents_5++;
                        printf("5 cents : %d \n", cents_5);
                    }
                }
            }
            else if(ptr != myvar->name)
            {
                printf("Not founded\n");
            }
        }
        else if(x ==2)
            break;
    }
    
    
    return(0);
}

'''

1 个答案:

答案 0 :(得分:0)

这似乎是错误的:

        for(i=0; i<idx; i++)
        {
            ptr = strstr(myvar->name, find_name);
        }

myvar 是一个数组,但您不使用数组索引。换句话说 - 您总是使用数组的第一个元素(即 myvar[0].name)。

除此之外,我希望当 strstr 返回非 NULL 值时循环应该停止。

您是否打算这样做:

        for(i=0; i<idx; i++)
        {
            ptr = strstr(myvar[i].name, find_name);
            if (ptr != NULL) break;
        }

(顺便说一句 - 你真的想使用 strstr 吗?我希望使用 strcmp 进行字符串比较)。

以上代码之后的所有代码都存在同样的问题,即缺少数组索引。

顺便说一句:您从文件中读取的方式并非 100% 正确。见Why is “while ( !feof (file) )” always wrong?