Thread: sscanf string parsing

  1. #1
    Registered User penney's Avatar
    Join Date
    Jan 2003
    Posts
    47

    Unhappy sscanf string parsing

    I am trying to parse data from a string. I know that the first thing that I want from the string is a string as well, but I want to limit the number of characters extracted from the string. Here's what I tried but it doesn't work - what am I doing wrong or what's the correct way or best way to handle this:

    <code>
    if (sscanf(somestring,"%*s%d",STRINGSIZE,tostr,&toint )==2)
    </code>

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    27
    Hi,
    Below is a sample that puts the first four char's form one string into another string.

    #include <stdio.h>
    #include <conio.h>

    int main()
    {
    char *mystr = "this is the input string";
    char frstfour[5];
    int x;

    frstfour[4] = 0; // string terminator
    for(x = 0; x < 4;++x)
    {
    frstfour[x] = mystr[x];
    }
    printf("\n%s\n",frstfour);

    getch();
    return 0;
    }

    Maybe you can get some ideas from it.
    lol
    Pappy
    You learn something new everyday.

  3. #3
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    Assuming your string is something harder like STR10, you can easily do it like this
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main(void)
    {
      int toint;
      char tostr[4] = {0};
      char *src = "STR10", *pos = src;
    
      strncpy(tostr, pos, 3);
      toint = atoi(pos + 3);
    
      printf("The string is %s\nThe number is %d\n", tostr, toint);
    
      return 0;
    }
    If there's some delimiting character between the string and the number then it's way easier :-)
    Code:
    #include <stdio.h>
    
    int main(void)
    {
      int toint;
      char tostr[4] = {0};
      char *src = "STR 10";
    
      if (sscanf(src, "%s %d", tostr, &toint) == 2)
      {
        printf("The string is %s\nThe number is %d\n", tostr, toint);
      }
    
      return 0;
    }
    >>if (sscanf(somestring,"%*s%d",STRINGSIZE,tostr,&toint )==2)
    For the scanf family, the * modifier means to read and discard that item, so your call reads everything up to whitespace or the end of the string and just throws it away. That's a problem with scanf and printf, their modifiers don't match and that causes confusion :-)
    *Cela*

  4. #4
    Registered User penney's Avatar
    Join Date
    Jan 2003
    Posts
    47

    Thanks Cela --- However....

    Would the following limit my input string to 10 characters?:

    Code:
    #include <stdio.h>
    int main(void)
    {
          /* ABCDEFGHIJKL is > 10 chars long and then a number follows */
          char *input_str="ABCDEFGHIJKL 123";
          char tostr[11];
          int num;
    
          if(sscanf(input_str,"%10s%d",tostr,&num)==2)
          {
                    printf("String limited to 10 = %s\n",tostr);
                    printf("Number = %d\n",num);
          }
    }

  5. #5
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>Would the following limit my input string to 10 characters?
    Yes, but sscanf would fail because there are more than 10 non-numeric characters, after reading 10 there would still be some for the next item to choke on :-)
    *Cela*

  6. #6
    Registered User penney's Avatar
    Join Date
    Jan 2003
    Posts
    47

    Thanks again!

    That's what I needed to know. I'm disappointed though that I have to hard code in the 10 to limit the string size instead of being able to use the * and a constant to perform the limiting. But I do want the sscanf to fail for any string larger than 10 characters.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. sscanf and string handling question
    By ursula in forum C Programming
    Replies: 14
    Last Post: 05-30-2009, 02:21 AM
  3. getopt - parsing string as argument
    By bertazoid in forum C Programming
    Replies: 13
    Last Post: 02-05-2009, 04:35 PM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  5. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM