-->
Page 1 of 2

String compare

PostPosted: Tue Dec 09, 2014 2:40 pm
by alonewolfx2
how can i compare string in c? i am using char compare and it works but i cant compare string. here is code. can anybody help me?
i am using freertos sdk and i am sending "ASD" over tcp and its working. but , i am sending "testpart" and its not working. what am i missing?
Code: Select all/* working part*/
char *recv_buf = (char *)zalloc(128);
while ((recbytes = read(client_sock , recv_buf, 128)) > 0) {

     recv_buf[recbytes] = 0;
      printf("S > read data success %d!\nS > %s\n", recbytes, recv_buf);

      if(recv_buf[0] == 'A' && recv_buf[1] == 'S' && recv_buf[2] == 'D'){
         gpio_output_set(BIT2, 0, BIT2, 0);
      }

/*unworking part*/
char *pbuf = (char *)zalloc(128);
sprintf(pbuf, "%s\n", recv_buf);
                if(strcmp(pbuf, "testpart") == 0)
{
gpio_output_set(0,BIT2, 0, BIT2);
}      


Re: String compare

PostPosted: Tue Dec 09, 2014 2:48 pm
by kingfisher
Try to compare with "testpart\n" or compare the original recv_buf

Re: String compare

PostPosted: Tue Dec 09, 2014 3:06 pm
by alonewolfx2
kingfisher wrote:Try to compare with "testpart\n" or compare the original recv_buf

I am sending direct string without "\n" I will try your suggestions and I will write again.

Re: String compare

PostPosted: Tue Dec 09, 2014 3:16 pm
by kingfisher
you are adding \n to the original string with
sprintf(pbuf, "%s\n", recv_buf);
You can compare the original buffer like this

if(strcmp(recv_buf, "testpart") == 0)
{
gpio_output_set(0,BIT2, 0, BIT2);
}

And omit the previous two lines