Thread: sscanf?

  1. #1
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732

    sscanf?

    Hi, Can anyone help me out in guiding me on how to extract this MAC address from the following string. I dunno how to get ride of this space in sscanf format specifier expression. The regular expression don't seems to fetch things properly.

    Code:
    #include <stdio.h>
    int main()
    {
        char str[] = "eth0      Link encap:Ethernet  HWaddr 00:80:66:10:C0:4D  inet addr:200.168.1.103  Bcast:200.168.1.255  Mask:255.255.255.0";
        char output[80];
        
        if( sscanf( str, "&#37;*s HWaddr %s %*s", output) == 1)
            printf("%s", output);
        
        getchar();
        return 0;
    }
    Any guidance would me much appreciated.

    Thanks a lot

    ssharish

    EDIT: The only problem which I tent to have is ti ignore the spaces. For example after eth0 you have got quite a lot of spaces. How would you eliminate those spaces. I tried

    Code:
    while ( sscanf(ptr, "%[^ ]%n", field, &n) == 1 ) 
    and
    while ( sscanf(ptr, "%[^ ]+%n", field, &n) == 1 ) Something like that
    So that I can ignore the spaces.
    Last edited by ssharish2005; 12-30-2007 at 10:09 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    sscanf doesn't have regular expressions. It has character classes, but even that is just a pale imitation of just one of the things a proper RE parser can do.

    For instance, all the first "%*s" matches in your example this "eth0". The whole thing then collapses when the H (of HWaddr) in your format doesn't match the L (of Link) in your input string.

    Try something like this, to step over the first part of the string, then parse it.
    Code:
    if ( (p=strstr(str, "HWaddr")) != NULL &&
          sscanf( p, "HWaddr %s", output) == 1)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Salem, thanks for your help. That worked.

    ssharish
    Last edited by ssharish2005; 01-01-2008 at 07:32 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sscanf and string handling question
    By ursula in forum C Programming
    Replies: 14
    Last Post: 05-30-2009, 02:21 AM
  2. Problem using sscanf fgets and overflow checking
    By jou00jou in forum C Programming
    Replies: 5
    Last Post: 02-18-2008, 06:42 AM
  3. Problems reading formatted input with sscanf
    By Nazgulled in forum C Programming
    Replies: 17
    Last Post: 05-10-2006, 12:46 AM
  4. sscanf question
    By Zarkhalar in forum C++ Programming
    Replies: 6
    Last Post: 08-03-2004, 07:52 PM
  5. sscanf (I think)
    By RyeDunn in forum C Programming
    Replies: 7
    Last Post: 07-31-2002, 08:46 AM