1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Sscanf()?

Discussion in 'Plugins' started by vefatica, Mar 27, 2011.

  1. vefatica

    Joined:
    May 20, 2008
    Messages:
    7,939
    Likes Received:
    30
    This

    Code:
        else if ( Sscanf(L"foo", L" %lf %2s", &number, &unit) == 2 )
        {
            Printf(L"%lf %s\r\n", number, unit);
    gives this:

    Code:
    0.0000000000000000 fo
    I would not have expected Sscanf() to return 2.

    Please explain how it works.
     
  2. rconn

    rconn Administrator
    Staff Member

    Joined:
    May 14, 2008
    Messages:
    9,854
    Likes Received:
    83
    Scanning the first argument returns immediately (with a 0, since the source
    wasn't a digit), and it then scans for the second argument. Passing a
    non-numeric argument will not terminate all subsequent processing.
     
  3. vefatica

    Joined:
    May 20, 2008
    Messages:
    7,939
    Likes Received:
    30
    On Sun, 27 Mar 2011 16:17:21 -0400, you wrote:

    |---Quote---
    |> I would not have expected Sscanf() to return 2.
    |---End Quote---
    |Scanning the first argument returns immediately (with a 0, since the source
    |wasn't a digit), and it then scans for the second argument. Passing a
    |non-numeric argument will not terminate all subsequent processing.

    Code:
    
        else if ( Sscanf(L"foo", L" %lf %2s", &number, &unit) == 2 )
        {
            Printf(L"%lf %s\r\n", number, unit);
    
    gives this:
    
    0.0000000000000000 fo
    But why did the whole thing wind up returning 2?
     
  4. rconn

    rconn Administrator
    Staff Member

    Joined:
    May 14, 2008
    Messages:
    9,854
    Likes Received:
    83
    Because it assigned 0 to number and "fo" to unit.
     
  5. vefatica

    Joined:
    May 20, 2008
    Messages:
    7,939
    Likes Received:
    30
    On Sun, 27 Mar 2011 18:25:52 -0400, you wrote:

    |---Quote---
    |> But why did the whole thing wind up returning 2?
    |---End Quote---
    |Because it assigned 0 to number and "fo" to unit.

    That's unlike the RTF's swscanf() (isn't it?). I can live with it but I'll have
    to learn that Sscanf() return value alone isn't sufficient to determine if all
    the numeric fields specified in the format string were actually found in the
    target string. Do you use that feature (zeroing/counting numeric fields that
    were specified but not found) to advantage?
     
  6. rconn

    rconn Administrator
    Staff Member

    Joined:
    May 14, 2008
    Messages:
    9,854
    Likes Received:
    83
    WAD -- Sscanf is different in dozens of ways from the RTL version.

    If I wanted RTL behavior, I would have used the RTL! Every difference in Sscanf is deliberate because I needed it for a particular use -- if you need RTL behavior, use the RTL (or write your own).
     
  7. Jim Cook

    Joined:
    May 20, 2008
    Messages:
    604
    Likes Received:
    0
    Does that Sscanf support the %n? I use int endofstring; sscanf(buf, "......
    %n", ....., &endofstring) and check buf[endofstring] for '\0'

    On Sun, Mar 27, 2011 at 19:18, rconn <> wrote:




    --
    Jim Cook
    2011 Tuesday: 4/4, 6/6, 8/8, 10/10, 12/12 and 5/9, 9/5, 7/11, 11/7.
    Next year they're Wednesday.
     
  8. rconn

    rconn Administrator
    Staff Member

    Joined:
    May 14, 2008
    Messages:
    9,854
    Likes Received:
    83
    Yes.
     

Share This Page