Q190209: BUG: Seek Returns Unexpected Value When File Opened for Append
Article: Q190209
Product(s): Microsoft Visual Basic for Windows
Version(s): WINDOWS:4.0,5.0,6.0
Operating System(s):
Keyword(s): kbGrpDSVB
Last Modified: 11-JAN-2001
-------------------------------------------------------------------------------
The information in this article applies to:
- Microsoft Visual Basic Learning Edition for Windows, versions 5.0, 6.0
- Microsoft Visual Basic Professional Edition for Windows, versions 5.0, 6.0
- Microsoft Visual Basic Enterprise Edition for Windows, versions 5.0, 6.0
- Microsoft Visual Basic Standard Edition, 32-bit, for Windows, version 4.0
- Microsoft Visual Basic Professional Edition, 16-bit, for Windows, version 4.0
- Microsoft Visual Basic Professional Edition, 32-bit, for Windows, version 4.0
- Microsoft Visual Basic Enterprise Edition, 16-bit, for Windows, version 4.0
- Microsoft Visual Basic Enterprise Edition, 32-bit, for Windows, version 4.0
-------------------------------------------------------------------------------
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.
Additional query words: kbDSupport kbDSD kbVBp kbvbp400bug kbVBp600bug kbVBp500bug kbVBA kbFileIO
======================================================================
Keywords : kbGrpDSVB
Technology : kbVBSearch kbAudDeveloper kbZNotKeyword6 kbZNotKeyword2 kbVB500Search kbVB600Search kbVBA500 kbVBA600 kbVB500 kbVB600 kbVB400Search kbVB400 kbVB16bitSearch
Version : WINDOWS:4.0,5.0,6.0
Issue type : kbbug
=============================================================================
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.