Thread: sscanf debug help

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    18

    Angry sscanf debug help

    Can someone please help me.
    I don't understand why this doesn't work.

    Code:
    char *line, *tag, *a, *b;
    int    n;
    
    strcpy(line, "This is an order for (XYZ-2) some order");
    tag = strchr(line, '(');
    strcpy(line, &tag[1]); // get rid of all text before and including (
    sscanf(line, "%s%[-]%d%[)]%s",a, n, b);
    should work out to:
    a = "XYZ"
    b = "some order"
    n = 2

    I tried
    sscanf(line, "%s-%d)%s",a, n, b);
    but it does the same thing.

    wolf

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    How about allocating some memory for line. Sure, you CAN just copy data into random uninitialized pointers, but that's what we here call a BadThing(TM).

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    18

    still no luck

    Code:
    char line[40], *tag, a[40], b[40];
    int    n;
    
    strcpy(line, "This is an order for (XYZ-2) some order");
    tag = strchr(line, '(');
    strcpy(line, &tag[1]); // get rid of all text before and including (
    sscanf(line, "%s%[-]%d%[)]%s",a, n, b);
    wolf

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If you are trying to scan for data and have it not placed into variables, you need to use the * symbol. Example:

    sscanf( somedata, "%s %*d", mystring );

    This actually scans both variables, but only stores the first one. You don't need to do all of what you're doing:

    ie: "%[-]"

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    18

    ugh

    Code:
    sscanf(line,"%s %*d %*s",a, n, b);
    no change.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    "no change" tells me nothing. Give me an example of what input you want, what output you expect, and what is stored in what variables, and I'll tell you why it doesn't work.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Feb 2002
    Posts
    18

    you're right

    I should be more specific. Sorry.

    using this code
    Code:
    char line[40], *tag, a[40], b[40];
    int    n = -1;
    
    strcpy(line, "This is an order for (XYZ-2) some order");
    tag = strchr(line, '(');
    strcpy(line, &tag[1]); // get rid of all text before and including (
    sscanf(line, "%s%d%s",a, n, b);
    results in
    a = "XYZ-2)"
    n = -1
    b = uninitialized character array

    I suspect it has something to do with a lack of whitespace between the XYZ and the integer. Am I way off here?

    wolf

  8. #8
    Registered User
    Join Date
    Jul 2002
    Posts
    13
    YOu're dead on. The scanf family uses whitespace to find separation of fields. So it won't even look for a digit until the first whitespace. That method will not work as is. I'd find a way to break it into 2 strings, a and b, and then break a into the digit and the string with strchr looking for the -.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    results in
    a = "XYZ-2)"
    n = -1
    b = uninitialized character array

    I suspect it has something to do with a lack of whitespace between the XYZ and the integer. Am I way off here?

    Given this string:

    "This is an order for (XYZ-2) some order"

    You jump ahead to:

    "X"

    From there:

    sscanf( buffer, "%s", buffer1 );

    This should give you:

    "XYZ-2" in buffer1, and ignore the rest.

    There is no integer after the 2, so why are you scanning for one? Since apparently you don't want the remainder of the string anywhere else, don't bother scanning for it. Otherwise, just do the same thing you did before, and use 1+strchr( line, ')' ) to skip past it.

    Just keep in mind what you want and take it a chunk at a time. The scanf functions are really only good for precicely formatted data. The down side of them is, when anything is not just so, you have to write the code to recover from it.

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Feb 2002
    Posts
    18

    agreed

    good for precise formatting.
    Not so in this case. I've got part of it to work using multiple strchr and atoi.

    Thanks quzah and Javariel. I do appreciate your help.

    wolf

  11. #11
    Registered User alex's Avatar
    Join Date
    Sep 2001
    Posts
    132
    Hi!

    Try to understand this:

    Code:
    char line[]="This is an order for (XYZ-2) some order";
    char a[50], c[50]; int b, n;
    
    n = sscanf(line, 
          "%*[^(]" /* skip up to "(" */
          "("      /* skip "(" */
          "%[^-]"  /* read up to "-" (into a) */
          "-"      /* skip "-"  */
          "%d"     /* read decimal integer (into b) */
          " "      /* skip whitespace */
          ")"      /* skip ")" */
          " "      /* skip whitespace */
          "%[^\n]",/* read up to newline (into c) */
          a, &b, c);
    
    if(n>0) printf("a = %s\n", a);
    if(n>1) printf("b = %d\n", b);
    if(n>2) printf("c = %s\n", c);
    alex

  12. #12
    Registered User
    Join Date
    Feb 2002
    Posts
    18

    makes sense

    when you put it like that. I understand now.
    Thanks for laying it out.

    wolf

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary not built with debug info - why?
    By ulillillia in forum C Programming
    Replies: 15
    Last Post: 12-11-2008, 01:37 AM
  2. makefiles - debug & release?
    By cpjust in forum C Programming
    Replies: 6
    Last Post: 10-26-2007, 04:00 PM
  3. Results in Debug and Release mode are different
    By jaro in forum C Programming
    Replies: 11
    Last Post: 05-27-2006, 11:08 AM
  4. Problems reading formatted input with sscanf
    By Nazgulled in forum C Programming
    Replies: 17
    Last Post: 05-10-2006, 12:46 AM
  5. Ask about Debug Assert Failed
    By ooosawaddee3 in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2002, 11:07 PM