Discussion:
sscanf has a bug in it (VS6.0)
(too old to reply)
Alan
2010-01-01 10:06:01 UTC
Permalink
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.

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.
David Lowndes
2010-01-01 11:03:26 UTC
Permalink
Post by Alan
I've just discovered a big in sscanf (I'm using C++ in VS 6.0).
I'm afraid not.
Post by Alan
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
Jochen Kalmbach [MVP]
2010-01-01 11:04:05 UTC
Permalink
Hi Alan!
Post by 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 ;)

If a leading zero is present, it will be interpreted as "octal":
060octal => 48decimal
Post by Alan
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/
Tim Roberts
2010-01-04 00:42:07 UTC
Permalink
Post by Jochen Kalmbach [MVP]
Hi Alan!
Post by 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, ***@probo.com
Providenza & Boekelheide, Inc.
Ulrich Eckhardt
2010-01-04 08:19:51 UTC
Permalink
Post by Alan
I've just discovered a big in sscanf (I'm using C++ in VS 6.0).
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
Loading...