Thread: Using sscanf to parse string

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    96

    Using sscanf to parse string

    I was wondering if it is possible to use sscanf() to parse an http response from a web server to look for particular values such as the error code for the http request, the server, content length, last-modified, and such. Is this possible to do after reading the file in or would I have to do this on a line by line basis as it is read in? Thanks!

  2. #2
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404
    Try strstr if you know the exact response.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    If you aquire it through an INET socket then sure, the http response is just a normal string in the sense of being null terminated, etc. You need to check this out yourself, maybe, http stuff has it's own style that's nothing to do with C, but I'm sure you would like packet sniffing.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    I would guess that sscanf function more often used to pick the specific parts of the string rather than check for any string pattern existence. Like for example the following code

    Code:
    #include <stdio.h>
    
    int main(void)
    {
       const char linestr[] = "HTTP/1.0 200 OK ";
       const char *ptr = linestr;
       char buffer[25];
       
       
       if( sscanf( ptr, "%*[^ ] %[^\n]", buffer ) )
           printf("Found!! -- > %s", buffer);
       else
           printf("Not found\n");
    
       getchar();
       return 0;
    }
    
    /* myn output
    Found!! -- >  200 OK
    */
    This can pick the string "200 OK" from the input string but then you will have to put some extra effort to see if you have got the right string. I might be wrong. If you want to check for like specific patterns then you might have to use some regular expression to achieve that. Or as suggested before use strstr function to find the string.

    -ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  2. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  3. Parse Error Before String Constant?
    By xombie in forum C Programming
    Replies: 4
    Last Post: 10-08-2004, 01:17 AM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM