Set File Position Method
The Set File Position method sets the position of the next read or write operation in an open file. It does not return a value. If you write to a file after reading beyond the end of the file, then this method increases the file length. If a read operation attempts to specify a negative or zero position, then Visual Basic returns an error message.
Format
Seek [#]filenumber, position
The following table describes the arguments that you can use with this method.
Argument | Description |
---|---|
filenumber |
The file number that the Open statement uses to open the file. For more information, see Open File Method. |
position |
An expression of type long that identifies the position depending on the following mode that this method uses to open the file:
The position is in the range of 1 through 2,147,483,647. The first byte or record in the file is at position 1, the second is at position 2, and so on. |
Example
The following example reads the contents of a sequential file line by line to a carriage return, and then displays the results. The CreateFile subroutine creates the c:\temp001 file that the main subroutine uses:
(general) (declarations)
Option Explicit
Declare Sub CreateFile
Sub CreateFile
Rem Put the numbers 10-100 into a file
Dim x as Integer
Open "c:\temp001" for Output as #1
For x = 10 to 100 step 10
Write #1, x
Next x
Close #1
End Sub
Sub Button_Click
Dim testscore as String
Dim x
Dim y
Dim newline
Call CreateFile
Open "c:\temp001" for Input as #1
x = 1
newline = Chr(10)
msgtext = "The test scores are: " & newline
Do Until x = Lof(1)
Line Input #1, testscore
x = x + 1
y = Seek(1)
If y>Lof(1) then
x = Lof(1)
Else
Seek 1,y
End If
msgtext = msgtext & newline & testscore
Loop
Close #1
Kill "c:\temp001"
End Sub