0

I have a string that looks like this:

"show net\r\r\nSSid=roving1\r\nChan=1\r\nAssoc=FAIL\r\nRate=12,   24Mb\r\nAuth=FAIL\r\nMode=NONE\r\nDHCP=FAIL\r\nBoot=0\r\nTime=OK\r\nLinks=0\r\n<4.00> "

This is data I receive from a peripheral device and I want to scan it for the string "Auth=FAIL". I'm querying it using the following command:

res = strstr(uart_rd, "Auth=FAIL");

however, it returns a value of 0. But if I simply issue the command:

res = strstr("nnAuth=FAILn", "Auth=FAIL");

it returns a value. What could be going wrong when it tries to search my char array uart_rd?

EDIT:

It looks like the issue was with a memset I was doing to clear the array before putting new data into it. I was filling it with 0s using the command:

memset(uart_rd,0,sizeof(uart_rd));

whereas I've now changed it to

memset(uart_rd,"",sizeof(uart_rd));

and it all seems to be working. Thanks for the help in diagnosing where my problem was though!

6
  • 4
    How is uart_rd declared? Commented Jan 9, 2014 at 9:28
  • strstr() certainly does work. If it can't find the substring in your array, then the substring isn't there. Maybe an encoding issue, or you accidentally pasted "invisible" characters either in the haystack or in the needle, or perhaps you have some other error at another place in your code (e. g. a buffer overflow that overwrites the contents of your array or whatnot). Commented Jan 9, 2014 at 9:31
  • Maybe is the \n before Auth what is causing the problem. Commented Jan 9, 2014 at 9:33
  • uart_rd is declared like this: char uart_rd[1000]; Commented Jan 9, 2014 at 9:35
  • Please give a sscce example which can be used to reproduce your problem. Commented Jan 9, 2014 at 9:40

1 Answer 1

2

Well, this code

    char uart_rd[] = "show net\r\r\nSSid=roving1\r\nChan=1\r\nAssoc=FAIL\r\nRate=12,   24Mb\r\nAuth=FAIL\r\nMode=NONE\r\nDHCP=FAIL\r\nBoot=0\r\nTime=OK\r\nLinks=0\r\n<4.00> ";
    char *res;
    res = strstr(uart_rd, "Auth=FAIL");
    printf("%s", res);

gives this output:

Auth=FAIL
Mode=NONE
DHCP=FAIL
Boot=0
Time=OK
Links=0
<4.00>

So you better check your declarations of strings.

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

3 Comments

I agree, i tried the same thing and it worked. I'm wondering if it's to do with the size of my array? My char array is declared as char uart_rd[1000], which gets filled with data from the peripheral. At some times, there is a lot of data, hence the large size. So when it gets filled with the data above, it puts it about 90 positions in, and the rest of the array is filled with 0s. Is it possible the size of the array is throwing up issues or something?
No, I don't think so. In the code above, uart_rd with size 1000 will give the same output. How did you declare res variable? Can you show it?
It looks like the issue was with a memset I was doing to clear the array before putting new data into it. I was filling it with 0s using the command: memset(uart_rd,0,sizeof(uart_rd)); whereas I've now changed it to memset(uart_rd,"",sizeof(uart_rd)); and it all seems to be working. Thanks for the help in diagnosing where my problem was though!

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.