Seek 函数Seek function

返回一个Long类型的值, 指定使用**Open** 语句打开的文件内的当前读/写位置。Returns a Long specifying the current read/write position within a file opened by using the Open statement.

语法Syntax

Seek(filenumber)Seek(filenumber)

必需的_filenumber_ 参数是一个包含有效文件号整数The required filenumber argument is an Integer containing a valid file number.

备注Remarks

Seek返回一个介于1和 2147483647 (相当于 2 ^ 31-1) 的值 (包括这两个值)。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, Output, Append, Input 将发生下一操作的字节位置。Byte position at which the next operation takes place. 文件的第一个字节位于位置 1,第二个字节位于位置 2,依此类推。The first byte in a file is at position 1, the second byte is at position 2, and so on.

示例Example

此示例使用 Seek 函数返回当前文件位置。This example uses the Seek function to return the current file position. 该示例假定TESTFILE是包含用户定义类型Record的记录的文件。The 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

对于以随机模式打开的文件, Seek将返回下一条记录的编号。For files opened in Random mode, Seek returns the number of the next record.

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 将返回将发生下一操作的字节位置。For files opened in modes other than Random mode, Seek returns the byte position at which the next operation takes place. 假定TESTFILE是包含几行文本的文件。Assume that TESTFILE is a file containing a few lines of text.

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.

另请参阅See also

支持和反馈Support and feedback

有关于 Office VBA 或本文档的疑问或反馈?Have questions or feedback about Office VBA or this documentation? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.