KnowledgeBase Archive

An Archive of Early Microsoft KnowledgeBase Articles

View on GitHub

Q60139: LEN Function Returns Wrong Length in LEFT$ in OPEN Statement

Article: Q60139
Product(s): See article
Version(s): 7.00   | 7.00
Operating System(s): MS-DOS | OS/2
Keyword(s): ENDUSER | SR# S900319-95 buglist7.00 fixlist7.10 | mspl13_basic
Last Modified: 20-SEP-1990

The LEN function in Microsoft BASIC Professional Development System
(PDS) version 7.00 may return an incorrect string length when used
within a string function in an OPEN statement. This occurs only when
the program is compiled with the Far Strings (BC /Fs) option, and
doesn't occur in the QuickBASIC Extended (QBX.EXE) environment or when
the program is compiled without the Far Strings option (in other
words, compiled with the BC.EXE default near strings).

To work around this problem, use a temporary variable for the result
of the LEN function and use that result in the string function.

Microsoft has confirmed this to be a problem with BC /Fs in Microsoft
BASIC Professional Development System (PDS) version 7.00. This problem
was corrected in BASIC PDS 7.10.

This problem occurs because the compiler incorrectly assumes that the
length will be the first 2 bytes of the descriptor when LEN is used in
a string function in the OPEN statement. This assumption is correct
for near strings, but the far string descriptor is different and the
length must be retrieved in a different manner.

Code Example
------------

The following are the compile and link lines that reproduce the
problem in the code example:

   BC LENTEST /Fs;
   LINK LENTEST;

The following code example OPENs the wrong file on the first OPEN
statement:

   ' LENTEST.BAS
   tmp$ = "TEST.12X"
   l% = LEN(tmp$)        'Temporary for work-around

   'This should incorrectly create a file named 'TEST.12X'
   OPEN LEFT$(tmp$, LEN(tmp$) - 1) FOR RANDOM AS #1
   CLOSE #1

   'For a workaround, use temporary variable (l%) for LEN(tmp$) and
   'you will get 'TEST.12'
   OPEN LEFT$(tmp$, l% - 1) FOR RANDOM AS #1
   CLOSE #1

Both OPEN statements in the above code example should open "TEST.12",
but the first OPEN actually opens "TEST.12X" because the string length
is returned incorrectly and is thus too large. Subtracting 1 from this
larger value still leaves the full string to be returned from LEFT$.

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.