DOCUMENT:Q190209 07-AUG-1998 [vbwin] TITLE :BUG: Seek Returns Unexpected Value When File Opened for Append PRODUCT :Microsoft Visual Basic for Windows PROD/VER:WINDOWS:4.0,5.0,6.0 OPER/SYS:WINDOWS KEYWORDS: ====================================================================== --------------------------------------------------------------------- The information in this article applies to: - Microsoft Visual Basic Learning, Professional, and Enterprise Editions for Windows, versions 5.0, 6.0 - Microsoft Visual Basic Standard, Professional, and Enterprise Editions, 16-bit and 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 ====================================================================== Version : WINDOWS:4.0,5.0,6.0 Platform : WINDOWS 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 1998.