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.

The My feature gives you greater productivity and performance in file I/O operations than Seek. For more information, see My.Computer.FileSystem Object.

Public Overloads Function Seek( _
   ByVal FileNumber As Integer _
) As Long
' -or-
Public Overloads Sub Seek( _
   ByVal FileNumber As Integer, _
   ByVal Position As Long _
)

Parameters

  • 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

Exception type

Error number

Condition

IOException

52

FileNumber does not exist.

IOException

54

File mode is invalid.

See the "Error number" column if you are upgrading Visual Basic 6.0 applications that use unstructured error handling. (You can compare the error number against the Number Property (Err Object).) However, when possible, you should consider replacing such error control with Structured Exception Handling Overview for Visual Basic.

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.
   Dim ID As Integer 
   Dim Name As String 
End Structure

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

FileOpen(1, "TESTFILE", OpenMode.Random)
Do While Not EOF(1)   
   WriteLine(1,Seek(1))   ' Write record number.
   FileGet(1, MyRecord, -1)   ' 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.

' Report character position at beginning of each line. 
Dim TextLine As String
FileOpen(1, "TESTFILE", OpenMode.Input)   ' Open file for reading.
While Not EOF(1)   
' Read next line.
   TextLine = LineInput(1)
   ' Position of next line.
   MsgBox(Seek(1))
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 TestRecord
   Dim Name As String 
   Dim ID As Integer 
End Structure

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 = "This is a test string." 
' Open file for output.
FileOpen(1, "TESTFILE", OpenMode.Input)
' Move to the third character.
Seek(1, 3)
Input(1, someText)
Console.WriteLine(someText)
FileClose(1)

Smart Device Developer Notes

This function is not supported.

Requirements

Namespace: Microsoft.VisualBasic

**Module:**FileSystem

Assembly: Visual Basic Runtime Library (in Microsoft.VisualBasic.dll)

See Also

Reference

FileGet Function

Loc Function

FileOpen Function

FilePut Function

IOException

Other Resources

Reading from Files in Visual Basic

Writing to Files in Visual Basic