BUG: Input Function Doesn't Ignore EOF

Last reviewed: February 26, 1996
Article ID: Q140354
The information in this article applies to:
  • Standard, Professional and Enterprise Editions of Microsoft Visual Basic for Windows, 16-bit and 32-bit, version 4.0
  • Standard and Professional Editions of Microsoft Visual Basic for Windows, version 3.0

SYMPTOMS

With Visual Basic 4.0, code that uses the Input$ function and assumes that Visual Basic will return an empty value if input is attempted past the end of file fails. This code returns run-time error number 62 - "Input past end of file". If run with Visual Basic 3.0, no error is received.

WORKAROUND

Users should not assume that Visual Basic will return an empty value if a program attempts to access a file location that doesn't exist because it is past the end of file location. The Lof function can be used to determine the length of a file, and no input should be attempted past this value. To fix code that currently does this, an explicit check for the end of file can be added before attempting to read data. This can be performed by using the Loc and Lof functions to check whether the file location (Loc) after an upcoming read will be greater than the length of file (Lof). The example below includes code that implements this fix.

STATUS

Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. We are researching the problem and will post more information here in the Microsoft Knowledge Base when it is available.

MORE INFORMATION

Steps to Reproduce

  1. Start Visual Basic, or if it already running, click New Project on the File menu.

  2. Add a new command button on the form.

  3. Insert the following code in the Command1_Click event:

       Private Sub Command1_Click ()
           Dim string1 As String * 20
           Dim string2 As String * 4
           Dim i As Integer
    
           Open "test.dat" For Output As #1
           For i = Asc("A") To Asc("Z")
               Print #1, Chr$(i);
           Next i
           Close #1
    
           Open "test.dat" For Binary As #1
           Do
               'If (Loc(1) + 20) > LOF(1) Then Exit Sub
               string1 = Input$(20, 1)
               Debug.Print string1
               'If (Loc(1) + 4) > LOF(1) Then Exit Sub
               string2 = Input$(4, 1)
               Debug.Print string2
           Loop Until EOF(1)
           Close #1
       End Sub
    
    

  4. Press the F5 key, or click Start on the Run menu (ALT, R, S) to run the project.

  5. Click the command button on the form. The error mentioned above is generated on the second iteration of the loop, before the first uncommented statement. The above code will work without error with Visual Basic 3.0, causing the following sequence of assignments:

          string1 = "ABCDEFGHIJKLMNOPQRST"
          string2 = "UVWX"
    
          string1 = "YZ                  "
          string2 = "    "
    
       Uncommenting the commented lines in the above procedure causes Visual
       Basic to check whether the next call to Input$ will read past the end of
       the file, avoiding the error. Additional code would be required to mimic
       exactly the functionality of Visual Basic 3.0. After determining that
       the next call to Input$ would read past the end of file, this new code
       would call Input$, but only read up to the end of file, not past it.
       Since this isn't implemented in the example code, the procedure exits
       before the last two characters ("YZ") are read.
    
    

REFERENCES

For additional information, please see the following article in the Microsoft Knowledge Base:

ARTICLE-ID: Q142246

TITLE     : BUG: Sequential File I/O Does Not Handle Embedded Nulls


Additional reference words: buglist4.00 4.00 vb4all vb4win
KBCategory: kbbuglist kbprg
KBSubcategory: IAPVBA


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.

Last reviewed: February 26, 1996
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.