VB6Parse / Library / File Operations / seek

VB6 Library Reference

Seek Statement

Sets the position for the next read or write operation in a file opened using the Open statement.

Syntax

Seek [#]filenumber, position

Parts

Remarks

Position Interpretation by File Mode

File Mode Position Represents
Random Record number (1-based)
Binary Byte position (1-based)
Input Byte position (1-based)
Output Byte position (1-based)
Append Byte position (1-based)

Examples

Seek to Beginning of File

Open "DATA.TXT" For Binary As #1
Seek #1, 1   ' Position at first byte
' Read or write operations
Close #1

Seek to Specific Byte Position

Open "BINARY.DAT" For Binary As #1
Seek #1, 100   ' Position at byte 100
Get #1, , myData
Close #1

Seek to Specific Record in Random File

Type Employee
ID As Integer
Name As String * 30
End Type

Dim emp As Employee
Open "EMPLOYEE.DAT" For Random As #1 Len = Len(emp)
Seek #1, 5   ' Position at record 5
Get #1, , emp
Close #1

Seek Based on Calculation

Dim recordNumber As Long
recordNumber = 10
Seek #1, recordNumber

Using Seek with Loop

Open "DATA.BIN" For Binary As #1
For i = 1 To 100 Step 10
Seek #1, i
Put #1, , dataArray(i)
Next i
Close #1

Seek to End of File

Open "APPEND.TXT" For Binary As #1
Seek #1, LOF(1) + 1   ' Position after last byte
Put #1, , newData
Close #1

Combined with Seek Function

Open "DATA.TXT" For Binary As #1
currentPos = Seek(1)      ' Get current position
Seek #1, currentPos + 50  ' Move 50 bytes forward
Close #1

Rewind File

Sub RewindFile(fileNum As Integer)
Seek fileNum, 1  ' Return to beginning
End Sub

Seek in Random Access Processing

Type Product
Code As String * 10
Price As Double
End Type

Dim prod As Product
Dim recordNum As Long

Open "PRODUCTS.DAT" For Random As #1 Len = Len(prod)
recordNum = 25
Seek #1, recordNum
Get #1, , prod
prod.Price = prod.Price * 1.1  ' Increase price by 10%
Seek #1, recordNum
Put #1, , prod
Close #1

Common Errors

Performance Tips

See Also

References

← Back to File Operations | View all statements