Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

Sscanf()?

May
12,845
164
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.
 
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?
 
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?
 
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:


> ---Quote (Originally by vefatica)---
> That's unlike the RTF's swscanf() (isn't it?)
> ---End Quote---
>
> 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).
>
>
>
>
>



--
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.
 
Back
Top