简体   繁体   English

结构数组内部的strstr char数组

[英]strstr char array inside struct array

I have a struct defined as; 我有一个结构定义为;

struct player {

    int no, age;

    char name[20];  

} players[10];

Array is filled from file. 数组从文件填充。 What I try to do is, take input from user, add input to char array, send it to search(char lookup[]) function and strstr name field in a loop. 我想做的是,从用户那里获取输入,将输入添加到char数组,将其发送到search(char lookup [])函数,并在循环中返回strstr name字段。

EDİT: Sorry I corrected the order. EDİT:对不起,我更正了订单。 I'm trying to strstr in a loop. 我正在尝试循环播放。

char *p = strstr(players[x].name, inputFromUser);

but p is always null. 但是p始终为null。 How can I do this? 我怎样才能做到这一点?

Thanks in advance. 提前致谢。

EDIT - Code Added... 编辑-已添加代码...

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

struct player {

    int no, age;

    char name[20];  

} players[20];

void fillstruct(char *);
void search(char []);

int main(int argc, char *argv[])
{
    int arg;
    int c;        
    int d;           
    int i=0;        

    char a[100];
    char *filename = NULL;

    while((arg=getopt(argc, argv, "f:"))!=-1)
    {
        switch(arg)
        {
            case 'f':
                filename = optarg;
                fillstruct(filename);
                break;
            default:
                break;
        }
    }   
    while((c=fgetc(stdin))!=EOF)
    {
        if(c!=10)
        {
            a[i]=c;
            i++;
        }       
        else
        {
            a[i]='\0';
            search(a);      
            i=0;            
        }       
    }
    return 0;
}

void search(char a[])
{
    int i=0;
    int col;
    int found=0;
    char *p =NULL;
    while((i<20)&&(found==0))
    {
        p = strstr(a, players[i].name);
        if(p)
        {
            col = p-a;
            printf("\nPlayer '%s' found in '%s'.. Found index: %d", a, players[i].name, col);
            found=1;
        }   
        else
        {
            printf("\np=%s a=%s player[%d].name=%s", p, a, i, players[i].name);
        }
        i++;
    }   
}

void fillstruct(char *name)
{
    FILE *fp;
    char line[100];
    int i=0;

    fp = fopen(name, "r");
    if(fp==NULL)
    {   
        exit(1);
    }

    while(fgets(line, 100, fp)!=NULL)
    {
        players[i].no=i;
        strcpy(players[i].name, line);
fprintf(stdout, "\nplayer=%s", players[i].name);
        players[i].age=20;
        i++;
    }   
    fclose(fp); 
}

Added as answer as suggested by mic_e 根据mic_e的建议添加为答案

Assuming you're trying to search for a player name using the input from a user, you have the arguments of strstr in the reverse order. 假设您尝试使用来自用户的输入来搜索玩家名称,那么您拥有相反顺序的strstr参数。 Also note that strstr is case sensitive. 另请注意,strstr区分大小写。

char *p = strstr(players[x].name, inputFromUser); char * p = strstr(players [x] .name,inputFromUser);

像这样:

char *p = strstr(players[x].name, inputFromUser);

It should work, It's fail if your input is wrong let me expalain in simple 它应该可以工作,如果您输入的内容有误,那就失败了,让我简单地解释一下

int main()
{
   char *ret;
   char mystr[]="stack";
   char str[]="This is stack over flow string";

   ret = strstr(str, mystr);

   printf("The substring is: %s\n", ret);

   return(0);
}

Output is 输出是

The substring is: stack over flow string

That means 那意味着

This function returns a pointer to the first occurrence in str of any of the entire sequence of characters specified in mystr , or a null pointer if the sequence is not present in str . 此函数返回一个指针,该指针指向mystr指定的整个字符序列中str中第一次出现的指针;如果str不存在该序列,则返回空指针。

It case sensitive function means if try to search like 区分大小写的功能意味着如果尝试像

char mystr[]="Stack";//Note here first char is capital

And you got output like 你得到的输出像

The substring is: (null)

You can check your input and target string at your side by just printing content of it and verify it's correct or not. 您可以通过仅打印输入内容和目标字符串并验证其正确与否来检查输入和目标字符串。

printf("str1:%s str2:%s\n",players[x].name,inputFromUser)
char *p = strstr(players[x].name, inputFromUser);

I hope this clear your doubts. 希望这能清除您的疑虑。

That Should Work. 那应该工作。

I think You have the problem with file reading Which fills the data array. 我认为您在填充数据数组的文件读取时遇到问题。

Please make sure that data you filled into structure is Ok. 请确保您输入的数据正确。

And strstr returns address of the first Occurrence of the string1 in string2 where, strstr(string2, string1); 并且strstr返回string2中string1的第一次出现的地址,其中strstr(string2,string1);

fgets stores the \\n and then stops taking input. fgets存储\\n ,然后停止接受输入。

So suppose a player name is "user" , players[i].name will be equal to "user\\n" while a is "user" . 因此,假设玩家名称为"user"players[i].name将等于"user\\n"a"user"

So return of strstr is always NULL . 因此, strstr返回始终为NULL

Try this instead: 尝试以下方法:

p = strstr(players[i].name,a);

OR, remove the \\n after taking input from file by fgets : 或者,通过fgets从文件中输入后,删除\\n

while(fgets(line, 100, fp)!=NULL)
{
    players[i].no=i;
    strcpy(players[i].name, line);
    players[i].name[strlen(players[i].name)-1]='\0'; //add this line
    fprintf(stdout, "\nplayer=%s", players[i].name);
    players[i].age=20;
    i++;
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM