BUG: sscanf() Fails If String Is Longer Than 32KLast reviewed: July 17, 1997Article ID: Q83084 |
5.10 6.00 6.00a 6.00ax 7.00 | 5.10 6.00 6.00a | 1.00 1.50
MS-DOS | OS/2 | WINDOWSkbprg kbbuglist The information in this article applies to:
SYMPTOMSPassing a string buffer longer than 32K+2 to sscanf() will cause sscanf() to return -1. It doesn't matter what is being read from the buffer.
CAUSEThe sscanf() function is implemented in such a way that its buffer has the same restrictions as a file-stream buffer. Because file streams cannot have a buffer larger than 32K, sscanf() cannot accept a buffer larger than 32K.
RESOLUTIONYou can work around this problem by placing a "\0" character in the string buffer within the first 32K so that sscanf() sees a string buffer shorter than 32K. If you are reading the data from a file, you can work around this problem by using fscanf() and scanning the data directly from the file you are reading from.
STATUSMicrosoft has confirmed this to be a problem in the products listed at the beginning of this article. We are researching this problem and will post new information here in the Microsoft Knowledge Base as it becomes available. This is not a problem in Visual C++ 32-bit Edition.
MORE INFORMATIONThe following code sample reproduces the problem:
Sample Code
/* Compile options needed: none */ #include <stdio.h> #include <memory.h> int func ( unsigned bufsize );static char buffer[33000]; FILE *fptr;
void main (){ int result; unsigned bufsize = 32768U; while (((result = func (bufsize) ) >= 0) && (bufsize < 33000U)) bufsize++;}
int func ( unsigned bufsize ){ int result; char data; memset( buffer, 'A', bufsize ); buffer[bufsize] = '\0'; if (( result = sscanf( buffer, " %c", &data )) != 1) printf( "\nSSCANF error\n" ); printf( "bufsize is %u : sscanf returned %d\n", bufsize, result ); return result;}
|
Additional reference words: 1.00 1.50 5.10 6.00 6.00a 6.00ax 7.00
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |