Visual Basic Language Reference  

Seek Function

Returns a Long specifying the current read/write position within a file opened using the FileOpen function or sets the position for the next read/write operation within a file opened using the FileOpen function.

Public Overloads Function Seek( _
   ByVal FileNumber As Integer _
) As Long

-or-

Public Overloads Sub Seek( _
   ByVal FileNumber As Integer, _
   ByVal Position As Long _
)

Parameter

FileNumber
Required. An Integer containing a valid file number.
Position
Required. Number in the range 1 – 2,147,483,647, inclusive, that indicates where the next read/write operation should occur.

Exceptions/Errors

Exception type Error number Condition
IOException 52 FileNumber does not exist.
IOException 54 File mode is invalid.

Remarks

Seek returns a value between 1 and 2,147,483,647 (equivalent to 2^31 – 1), inclusive.

The following describes the return values for each file access mode:

Mode Return Value
Random Number of the next record read or written
Binary, Input, Output,
Append
Byte position at which the next operation takes place. The first byte in a file is at position 1, the second byte is at position 2, and so on.

Example

This example uses the Seek function to return the current file position. The example assumes TESTFILE is a file containing records of the structure Record.

Structure Record   ' Define user-defined type.
   ID As Integer
   Name As String
End Structure

For files opened in Random mode, Seek returns the number of next record.

Dim MyRecord As Record   
FileOpen(1, "TESTFILE", OpenMode.Random)
Do While Not EOF(1)   
   Debug.WriteLine(Seek(1))   ' Print record number.
   FileGet(1, MyRecord)   ' Read next record.
Loop
FileClose(1)  

For files opened in modes other than Random mode, Seek returns the byte position at which the next operation takes place. Assume TESTFILE is a file containing a few lines of text.

' Print character position at beginning of each line.
Dim TextLine As String
FileOpen(1, "TESTFILE", OpenMode.Input)   ' Open file for reading.
While Not EOF(1)   
   TextLine = LineInput(1)   ' Read next line.
   Debug.WriteLine(Seek(1))   ' Position of next line.
End While
FileClose(1) 

This example uses the Seek function to set the position for the next read or write within a file. This example assumes people.txt is a file containing records of the structure Record.

Structure Record
   Dim Name As String
   Dim ID As Integer
End Structure

For files opened in Random mode, Seek sets the next record.

Public Sub SeekAPerson(ByVal index As Integer)
   Try  
      FileOpen(1, "c:\people.txt", OpenMode.Random)
      Dim onePerson As Record
      Seek(1, index)
      FileGet(1, onePerson)
      FileClose(1)
      Console.WriteLine(onePerson.Name & " " & onePerson.ID)
   Catch
      ' Error recovery code here.
   End Try
End Sub

For files opened in modes other than Random mode, Seek sets the byte position at which the next operation takes place. Assume TESTFILE is a file containing a few lines of text.

Dim someText As String
FileOpen(1, "TESTFILE", OpenMode.Input)   ' Open file for output.
Seek(1, 3)   ' Move to the third character.
Input(1, someText)
Console.WriteLine(someText)
FileClose(1)

See Also

FileGet Function | Loc Function | FileOpen Function | FilePut Function | IOException