Q43219: scanf, sscanf, fscanf Functions Fail When Scanning All Zeros
Article: Q43219
Product(s): See article
Version(s): 2.00
Operating System(s): MS-DOS
Keyword(s): ENDUSER | buglist2.00 | mspl13_c
Last Modified: 2-MAY-1989
The functions scanf, sscanf, and fscanf fail when all of the following
conditions are satisfied:
1. The format specifier for a field is any of the following, where "w"
is the maximum input field width:
%wX %wI %wD
%wx %wi %wO
%wlx %wli %wU
2. The field being scanned is all zeros and has a length of w.
3. The field being scanned is immediately followed by a valid digit
(no intervening white space).
Under the above conditions, these functions read one extra digit from
the input buffer as if the "skip leading zeros" routine doesn't
realize that it is scanning past the maximum length specified (w).
To work around this problem, for the format specifiers %wD, %wO, and
%wU, use %wld, %wlo, and %wlu, respectively. For the remaining
specifiers, there is no workaround other than to scan the buffer
character by character.
Microsoft has confirmed this to be a problem in Version 2.00. We are
researching this problem and will post new information as it becomes
available.
The following program demonstrates this problem:
#include <stdio.h>
void main (void)
{ int a, b;
/* There are 2 zeros, and w=2. */
sscanf ( "0012", "%2x%d", &a, &b );
/* This should output: a=0 b=12
* Instead, it outputs: a=1 b=2
*/
printf ( "a=%x b=%d\n", a, b );
}
THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.
Copyright Microsoft Corporation 1986-2002.