Seek statement
Sets the position for the next read/write operation within a file opened by using the Open statement.
Syntax
Seek [ # ] filenumber, position
The Seek statement syntax has these parts:
Part | Description |
---|---|
filenumber | Required. Any 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. |
Remarks
Record numbers specified in Get and Put statements override file positioning performed by Seek.
Performing a file-write operation after 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.
Example
This example uses the Seek statement to set the position for the next read or write within a file. This example assumes that TESTFILE
is a file containing records of the user-defined type Record
.
Type Record ' Define user-defined type.
ID As Integer
Name As String * 20
End Type
For files opened in Random mode, Seek sets the next record.
Dim MyRecord As Record, MaxSize, RecordNumber ' Declare variables.
' Open file in random-file mode.
Open "TESTFILE" For Random As #1 Len = Len(MyRecord)
MaxSize = LOF(1) \ Len(MyRecord) ' Get number of records in file.
' The loop reads all records starting from the last.
For RecordNumber = MaxSize To 1 Step - 1
Seek #1, RecordNumber ' Set position.
Get #1, , MyRecord ' Read record.
Next RecordNumber
Close #1 ' Close file.
For files opened in modes other than Random mode, Seek sets the byte position at which the next operation takes place. This example assumes that TESTFILE
is a file containing a few lines of text.
Dim MaxSize, NextChar, MyChar
Open "TESTFILE" For Input As #1 ' Open file for input.
MaxSize = LOF(1) ' Get size of file in bytes.
' The loop reads all characters starting from the last.
For NextChar = MaxSize To 1 Step -1
Seek #1, NextChar ' Set position.
MyChar = Input(1, #1) ' Read character.
Next NextChar
Close #1 ' Close file.
See also
Support and feedback
Have questions or feedback about Office VBA or this documentation? Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.
피드백
https://aka.ms/ContentUserFeedback
출시 예정: 2024년 내내 콘텐츠에 대한 피드백 메커니즘으로 GitHub 문제를 단계적으로 폐지하고 이를 새로운 피드백 시스템으로 바꿀 예정입니다. 자세한 내용은 다음을 참조하세요.다음에 대한 사용자 의견 제출 및 보기