Chat freely about anything...

User avatar
By alonewolfx2
#4410 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);
}      

User avatar
By alonewolfx2
#4416
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.
User avatar
By kingfisher
#4418 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