Microsoft KB Archive/190209

= BUG: Seek Returns Unexpected Value When File Opened for Append =

Article ID: 190209

Article Last Modified on 6/29/2004

-

APPLIES TO


 * Microsoft Visual Basic 5.0 Learning Edition
 * Microsoft Visual Basic 6.0 Learning Edition
 * Microsoft Visual Basic 5.0 Professional Edition
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 5.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition
 * Microsoft Visual Basic 4.0 Standard Edition
 * Microsoft Visual Basic 4.0 Professional Edition
 * Microsoft Visual Basic 4.0 Professional Edition
 * Microsoft Visual Basic 4.0 16-bit Enterprise Edition
 * Microsoft Visual Basic 4.0 32-Bit Enterprise Edition

-



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
 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

 Create a new Visual Basic project. Form1 is created by default. Add a CommandButton, Command1, to Form1.  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  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.  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 </li> Repeat step 5, and note that the correct value is displayed.</li></ol>

Additional query words: kbDSupport kbDSD kbVBp kbvbp400bug kbVBp600bug kbVBp500bug kbVBA kbFileIO

Keywords: kbbug KB190209

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.