Seek 函数

返回一个 Long 类型的值,指定使用 Open 语句打开的文件中的当前读/写位置。

语法

查找 (filenumber)

所需的 filenumber参数是包含有效文件编号Integer

备注

Seek 返回一个介于 1 和 2,147,483,647 (等效于 2^31 - 1) (含)的值。

以下介绍每个文件访问模式的返回值。

模式 返回值
随机 读取或写入的下一条记录的编号。
BinaryOutputAppendInput 将发生下一操作的字节位置。 文件的第一个字节位于位置 1,第二个字节位于位置 2,依此类推。

示例

此示例使用 Seek 函数返回当前文件位置。 该示例假定 TESTFILE 是一个包含用户定义类型 Record的记录的文件。

Type Record    ' Define user-defined type.
    ID As Integer
    Name As String * 20
End Type

对于在随机模式下打开的文件, Seek 返回下一条记录的编号。

Dim MyRecord As Record    ' Declare variable.
Open "TESTFILE" For Random As #1 Len = Len(MyRecord)
Do While Not EOF(1)    ' Loop until end of file.
    Get #1, , MyRecord    ' Read next record.
    Debug.Print Seek(1)    ' Print record number to the Immediate window.
Loop
Close #1    ' Close file.

对于使用随机模式之外的模式打开的文件, Seek 将返回将发生下一操作的字节位置。 假设 是 TESTFILE 一个包含几行文本的文件。

Dim MyChar
Open "TESTFILE" For Input As #1    ' Open file for reading.
Do While Not EOF(1)    ' Loop until end of file.
    MyChar = Input(1, #1)    ' Read next character of data.
    Debug.Print Seek(1)    ' Print byte position to the Immediate window.
Loop
Close #1    ' Close file.

另请参阅

支持和反馈

有关于 Office VBA 或本文档的疑问或反馈? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。