1

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.

EDİT: Sorry I corrected the order. I'm trying to strstr in a loop.

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

but p is always 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); 
}
12
  • What are the values of inputFromUser and {players[x].name} that you tested? Commented Apr 1, 2014 at 13:10
  • Why the braces around players[x].name? Commented Apr 1, 2014 at 13:10
  • 1
    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, also note that strstr is case sensitive. Commented Apr 1, 2014 at 13:10
  • @imbtfab: I think you can write that as an answer -- even if the question is too imprecise to actually verify it. Commented Apr 1, 2014 at 13:11
  • as far as I know, strstr appends the 2nd string to the first, it does not return a value Commented Apr 1, 2014 at 13:11

5 Answers 5

2

Added as answer as suggested by 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. Also note that strstr is case sensitive.

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

Sign up to request clarification or add additional context in comments.

3 Comments

If p is null, then examine the input and players name. Is the case the same? E.g. You != you, and will return null. Does inputFromUser contain a trailing space or CR/LF etc?
No, inputFromUser has no white spaces or others and ended with LF.
Both inputFromUser and player name are properly null terminated? strstr as given is the proper use, if it doesn't match, then it's either the string you're searching or the user input that's not what you think it is. Use the debugger, single step through your for loop, and look at both player[x].name and inputFromUser where you think it should match then examine character by character.
0

fgets stores the \n and then stops taking input.

So suppose a player name is "user", players[i].name will be equal to "user\n" while a is "user".

So return of strstr is always NULL.

Try this instead:

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

OR, remove the \n after taking input from file by fgets:

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++;
} 

Comments

0

Like this:

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

1 Comment

It's the opposite way round from your question - the arguments were the wrong way around.
0

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.

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.

2 Comments

I know its case sensitive, the problem is not case sensitivity.
@user2237922 I just give you example. It's very good if you know about it.But my assumption is the way may be wrong from which you taking input. So can you post code for how you take input?
0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.