Thread: Sring help

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    26

    Sring help

    Code:
    if(strstr(buf,"PING"))
             {
                 sscanf(buf,"PING :%s",str);
                 printf("BOT WILL SEND BACK %s\n",str);
             }
    the contents of buf is received through a socket.
    buf is declared as char buf[1024];
    strstr finds PING but sscanf does not place it into str.
    In fact, str is blank.
    That is the correct format as well so the statement isn't wrong.
    Why would be str be coming back black?
    Thanks

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Is buf null terminated? What does it look like right before the call to sscanf?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    26
    Thats the problem I think I'm running into.
    It's not null terminated.
    How can I add it to the end of the string.
    I've tride for looping to the end and then add buf[j] = '\0'; but that doesn't change the outcome.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Keep track of what you've received and add a null terminator at the end?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    26
    Code:
    char *p;
    p = buf;
    while((*p++ = getchar()) != '\n');
    *p='\0';
    that should work but still doesn't give me anything back from sscanf

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    I thought you said the contents came from a socket?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Oct 2007
    Posts
    26
    They did.

    Code:
    sread = recv(s, buf, sizeof(buf),0); //contents stored in buf

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    buf[sread] = '\0'; /* ??? */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    Registered User
    Join Date
    Oct 2007
    Posts
    26
    That doesn't work either.
    I'm really at a lost right now.

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Hmm.
    Quote Originally Posted by Dave_Sinkula View Post
    What does it look like right before the call to sscanf?
    Could it be that PING doesn't start the string and you discard the return value from strstr?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  11. #11
    Registered User
    Join Date
    Oct 2007
    Posts
    26
    I have error checking on strstr and it's not there.
    I print out buf before parsing the string and yes PING starts.
    PING :blah.blah.blah

  12. #12
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
       const char buf[] = "yada yada yada PING: some text";
       char str[20];
       const char *ptr = strstr(buf,"PING");
       if ( ptr )
       {
          if ( sscanf(ptr, "PING :&#37;19s", str) == 1 )
          {
             printf("BOT WILL SEND BACK \"%s\"\n", str);
          }
       }
       return 0 ;
    }
    
    /* my input/output
    BOT WILL SEND BACK "some"
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed