Seek Function

See Also1R7FPZP              ExampleC48AR3>Low

Returns the current file position.


Seek (filenumber)


The argument filenumber is the number used in the Open statement to open the file.  You can use any numeric expression71RISN that evaluates to the number of an open file.

Seek returns a value between 1 and 2,147,483,647 (equivalent to 2^31-1), inclusive.  For files open in Random mode, Seek returns the number of the next record read or written.  For files opened in Binary, Output, Append, or Input mode, Seek returns the byte position at which the next operation is to take place.  The first byte in a file is at position 1, the second byte is at position 2, and so on.

See Also

Get StatementLANGET

Open Statement103I7PS

Put StatementLANPUT

Seek StatementQ2CHW7

Seek Function Example

The example uses the Seek statement to set the file position, and the Seek function to return the current file position.  To try this example, paste the code into the Declarations section of a form.  Then press F5 and click the form.


Sub Form_Click ()

   Dim NameField As String * 20             ' Declare form variable.

   Dim I, Max, Msg                          ' Declare variables.

   ' Create sample data file.

   Open "TESTFILE" For Random As #1 Len = Len(NameField)

   ' Get user input to fill records.

   For I = 1 To 3

      NameField = InputBox("Enter student name: ")

      Put #1, I, NameField                  ' Put record on disk.

   Next I

   Close #1                                 ' Close data file.

   ' Open data file.

   Open "TESTFILE" For Random As #1 Len = Len(NameField)

   Max = LOF(1) \ Len(NameField)            ' Calculate # of records.

   For I = Max To 1 Step -1                 ' Read file backwards.

      Seek #1, I                            ' Set file position.

      Get #1, , NameField                   ' Get record.

      Msg = "Record #" & (Seek(1) - 1) & " contains: "

      Msg = Msg & NameField

      MsgBox Msg                            ' Display student name.

   Next I

   Close #1                                 ' Close file.

   Msg = "Choose OK to remove the test file."

   MsgBox Msg                               ' Display message.

   Kill "TESTFILE"                          ' Remove file from disk.

End Sub