Sets the position for the next read or write within a file opened using the Open statement.
Seek [#]filenumber, position
The Seek statement syntax has these parts:
Part |
Description |
filenumber |
Any valid file number. |
position |
Number in the range 1 to 2,147,483,647, inclusive, that indicates where the next read or write should occur. |
Record numbers specified in Get and Put statements override file positioning performed by Seek.
Performing a file write after doing a Seek operation beyond the end of a file extends the file. If you attempt a Seek operation to a negative or zero position, an error occurs.
Get Statement, Open Statement, Put Statement, Seek Function.
This example uses the Seek statement to set the position for the next read or write within a file. This example assumes TESTFILE is a file containing records of the user-defined type Record.
Type Record ' Define user-defined type. ID As Integer Name As String * 20Type
For files opened in random-file mode, Seek sets the next record.
Dim MyRecord As Record ' Declare variable. ' Open file in random-file mode."TESTFILE" For Random As #1 Len = Len(MyRecord)= LOF(1) \ Len(MyRecord) ' Get number of records in file. ' The loop reads all records starting from the last.RecordNumber = MaxSize To 1 Step - 1 Seek #1, RecordNumber ' Set position. Get #1, , MyRecord ' Read record.RecordNumber#1 ' Close file.
For files opened in modes other than Random mode, Seek sets the byte position at which the next operation will take place. Assume TESTFILE is a file containing a few lines of text.
Open "TESTFILE" For Input As #1 ' Open file for input.= LOF(1) ' Get size of file in bytes. ' The loop reads all characters starting from the last.NextChar = MaxSize To 1 Step -1 Seek #1, NextChar ' Set position. MyChar = Input(1, #1) ' Read character.NextChar#1 ' Close file.