I'm using this to get information from a dialog, completed by a user.
sscanf(OptDlg.m_input, "%i", &Iret);
I can't control what users put in and a leading zero is valid in my book.
> I've just discovered a big in sscanf (I'm using C++ in VS 6.0). I'm trying to
> read an integer from a CString. If there is a leading zero, the integer is
> decoded incorrectly! For example, if the CString contains "60" it returns
> integer 60 but if the CString contains "060" it returns integer 48! I've
> searched the help file for any mention of prohibited leadign zeros but can't
> find anything so I guess it's a bug in the Microsoft library function.
This is not a bug, it is a feature ;)
If a leading zero is present, it will be interpreted as "octal":
060octal => 48decimal
> sscanf(OptDlg.m_input, "%i", &Iret);
You must not use "%i", instead use "%d".
This behaviour is well documented:
http://msdn.microsoft.com/en-us/library/6ttkkkhh.aspx
<quote>
An integer. Hexadecimal if the input string begins with "0x" or "0X",
octal if the string begins with "0", otherwise decimal.
</quote>
--
Greetings
Jochen
My blog about Win32 and .NET
http://blog.kalmbachnet.de/
I'm afraid not.
> I'm trying to
>read an integer from a CString. If there is a leading zero, the integer is
>decoded incorrectly! For example, if the CString contains "60" it returns
>integer 60 but if the CString contains "060" it returns integer 48! I've
>searched the help file for any mention of prohibited leadign zeros but can't
>find anything so I guess it's a bug in the Microsoft library function.
>
>I'm using this to get information from a dialog, completed by a user.
>
>sscanf(OptDlg.m_input, "%i", &Iret);
Taken from the MSDN documentation:
"i
An integer. Hexadecimal if the input string begins with "0x" or "0X",
octal if the string begins with "0", otherwise decimal.
"
Use %d
Dave
>Hi Alan!
>
>> I've just discovered a big in sscanf (I'm using C++ in VS 6.0). I'm trying to
>> read an integer from a CString. If there is a leading zero, the integer is
>> decoded incorrectly! For example, if the CString contains "60" it returns
>> integer 60 but if the CString contains "060" it returns integer 48! I've
>> searched the help file for any mention of prohibited leadign zeros but can't
>> find anything so I guess it's a bug in the Microsoft library function.
>
>This is not a bug, it is a feature ;)
Further, it's part of the C standard, and has been present in C since the
very beginning. It is not a Microsoft invention. You see the same thing
in code:
int x = 060;
"x" now contains 48 decimal.
Ah, kids today. They don't know any history...
--
Tim Roberts, ti...@probo.com
Providenza & Boekelheide, Inc.
Nobody mentioned that yet, VC6 (aka VS98) is now 12 years old, predates the
C++ standard and is not supported by the vendor. Upgrade.
Uli
--
C++ FAQ: http://parashift.com/c++-faq-lite
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932