scanf format string problem
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: scanf format string problem

  1. #1
    Join Date
    Dec 2006
    Posts
    60

    scanf format string problem

    I need to parse a line of text in this format, where required delimiters are bolded and the rest of the data need to be captured.

    Code:
    varname : 0|8@1- (1.0,0.0) [0.0|0.0] description
    my scanf looks like this:

    Code:
    struct {
    char varname[64];
    int int1;
    int int2;
    char char1;
    char char2;
    float floatx;
    float floaty;
    float range1;
    float range2;
    char description[64];
    }entry;
    
    
    sscanf_s(InputStr, "%64s %: %d%|%d%@%c%c %(%f%,%f%) %[%f%|%f%] %64s",
    entry.varname, 64,
    &entry.int1,
    &entry.int2,
    &entry.char1,
    &entry.char2,
    &entry.floatx,
    &entry.floaty,
    &entry.range1,
    &entry.range2,
    entry.description, 64);
    everything is captured except floatx, floaty, and description. can anyone provide some insight?
    Last edited by acerunner316; July 15th, 2011 at 08:12 PM.

  2. #2
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,571

    Re: scanf format string problem

    I'm too tired at the moment to pick that apart completely but the following struck my eyes:

    Apparently you have way too many % signs in your format string. The ones highlighted in red are at least superfluous. The one preceding the opening square bracket may also be mistaken as an undelimited string format specifier.

    Code:
    sscanf_s(InputStr, "%64s %: %d%|%d%@%c%c %(%f%,%f%) %[%f%|%f%] %64s",
    entry.varname, 64,
    &entry.int1,
    &entry.int2,
    &entry.char1,
    &entry.char2,
    &entry.floatx,
    &entry.floaty,
    &entry.range1,
    &entry.range2,
    entry.description, 64);
    The two parameters to scanf() highlighted in magenta seem to be field width specifications. Not only that I just know something like that from printf() and haven't found anything about that in the scanf() documentation, the field width also is already specified explicitly in the format string.

    Try to fix that first and see where it gets you. Feel free to report back if that doesn't solve the problem.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  3. #3
    Join Date
    Dec 2006
    Posts
    60

    Re: scanf format string problem

    i am actually using the "security enhanced" version of scanf, sscanf_s, which requires the additional size specification that u highlighted in magenta for strings, and (I just found out) for chars too. So that's one step closer to solving my problem.

    The additional % signs are intended. It means to look for the required character but is not part of the string to store. For example the part where I look for "%(%f%,%f%)" tells scanf to expect a string in the format "(1.234,5.678)", where the opening and closing parenthesis, and comma is required delimiters. Where I am having problems is the %[, which apparently is a special (reserved?) character that scanf doesn't like. If I did %*c, then it works fine. But %*c means ANY character can be in that position.
    Last edited by acerunner316; July 18th, 2011 at 04:26 PM.

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,885

    Re: scanf format string problem

    You are not correctly understanding the use of %.

    For example the part where I look for "%(%f%,%f%)" tells scanf to expect a string in the format "(1.234,5.678)", where the opening and closing parenthesis, and comma is required delimiters.
    To do that, all you need is this:
    Code:
    scanf("(%f,%f)",&f1,&f2);
    %[____] does indeed have a special meaning, although it isn't documented in too many places. It means "match anything within the brackets". Or, alternatively, "%[~_____]" means "match anything not within the brackets after the ~".

  5. #5
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,571

    Re: scanf format string problem

    Quote Originally Posted by acerunner316 View Post
    i am actually using the "security enhanced" version of scanf, sscanf_s, which requires the additional size specification [...].
    Oops! I simply overlooked that _s at the end of the function name. As I wrote: I was quite tired...

    The additional % signs are intended. It means to look for the required character but is not part of the string to store. [...]
    Regarding this, I second what Lindley posted. Perhaps for additional clarification: Any character in a format string that is neither a format specification nor whitespace means "expect exatly this character".
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  6. #6
    Join Date
    Dec 2006
    Posts
    60

    Re: scanf format string problem

    oh! thanks for the clarification. Indeed, the scanf documentation can be a bit lacking, and even confusing. I've seen examples that use the leading % for expected characters, like I have done, and others that does not.

    As for the square bracket, I have seen that before. But completely forgot about it. I think I've seen it used with a caret instead of a tilda though.

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,885

    Re: scanf format string problem

    Quote Originally Posted by acerunner316 View Post
    As for the square bracket, I have seen that before. But completely forgot about it. I think I've seen it used with a caret instead of a tilda though.
    You may be right. I was working from memory on the tilda thing (like I said most documentation doesn't even mention that).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center