BUG: Seek Returns Unexpected Value When File Opened for Append (190209)



The information in this article applies to:

  • Microsoft Visual Basic Learning Edition for Windows 5.0
  • Microsoft Visual Basic Learning Edition for Windows 6.0
  • Microsoft Visual Basic Professional Edition for Windows 5.0
  • Microsoft Visual Basic Professional Edition for Windows 6.0
  • Microsoft Visual Basic Enterprise Edition for Windows 5.0
  • Microsoft Visual Basic Enterprise Edition for Windows 6.0
  • Microsoft Visual Basic Standard Edition, 32-bit, for Windows 4.0
  • Microsoft Visual Basic Professional Edition, 16-bit, for Windows 4.0
  • Microsoft Visual Basic Professional Edition, 32-bit, for Windows 4.0
  • Microsoft Visual Basic Enterprise Edition, 16-bit, for Windows 4.0
  • Microsoft Visual Basic Enterprise Edition, 32-bit, for Windows 4.0

This article was previously published under Q190209

SYMPTOMS

When using the Seek method to reference the current byte position within a file opened for appending, you receive an incorrect value.

RESOLUTION

To get the proper byte position, determine the file's original size in bytes with the FileLen function before opening the file, and use this value as an adjustment to the value returned by the Seek method.

STATUS

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

MORE INFORMATION

Opening a file for Append allows you to add new data to an existing file without affecting the information already available in that file. "Byte Position" refers to the byte that will be read or written when the next file operation takes place. Within the file, the first byte is at position 1, the second byte is at position 2, and so on. Therefore, the initial value for the Seek method for a file opened for appending should be the size of that file plus 1.

In the products referenced at the beginning of this article, the Seek method always returns an incorrect value that is less than the actual number of bytes in the file. Earlier versions of Visual Basic, including version 3.0, did not exhibit this behavior.

Although the value reported by the Seek method is incorrect, the "Print" and "Write" methods are not affected by this number and data is written to the file correctly.

Steps to Reproduce Behavior

  1. Create a new text file, named "C:\TEST.TXT," using NOTEPAD.EXE. Type the following text into this new file and save the changes:

    Hello World

  2. Create a new Visual Basic project. Form1 is created by default.
  3. Add a CommandButton, Command1, to Form1.
  4. Paste the following code into Form1's code window:
          Private Sub Command1_Click()
             ' Open text file for appending
             Open "c:\test.txt" For Append As #1
    
             ' Display the current read/write position
             MsgBox Seek(1)
    
             ' Close the file
             Close #1
          End Sub
    						
  5. Run the sample application and click the button. The value of 1 is displayed indicating that the current byte position is at the beginning of the file. The correct value should be the size of the entire text file plus 1. In the case above, the result should be 12.
  6. Stop the project and replace the code in step 4 above with the following to demonstrate a workaround:
          Private Sub Command1_Click()
             Dim SizeOfFile As Long
    
             ' Determine size of file before opening
             SizeOfFile = FileLen("c:\test.txt")
    
             ' Open text file for appending
             Open "c:\test.txt" For Append As #1
    
             ' Display adjusted seek value
             MsgBox Seek(1) + SizeOfFile
    
             ' Close the file
             Close #1
          End Sub
    						
  7. Repeat step 5, and note that the correct value is displayed.

Modification Type:MajorLast Reviewed:6/29/2004
Keywords:kbbug KB190209