FileSystem.Seek Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Returns a Long
specifying the current read/write position in a file opened by using the FileOpen
function, or sets the position for the next read/write operation in a file opened by using the FileOpen
function. The My
feature gives you better productivity and performance in file I/O operations than Seek
. For more information, see FileSystem.
Overloads
Seek(Int32) |
Returns a |
Seek(Int32, Int64) |
Returns a |
Seek(Int32)
- Source:
- FileSystem.vb
- Source:
- FileSystem.vb
- Source:
- FileSystem.vb
Returns a Long
specifying the current read/write position in a file opened by using the FileOpen
function, or sets the position for the next read/write operation in a file opened by using the FileOpen
function. The My
feature gives you better productivity and performance in file I/O operations than Seek
. For more information, see FileSystem.
public:
static long Seek(int FileNumber);
public static long Seek (int FileNumber);
static member Seek : int -> int64
Public Function Seek (FileNumber As Integer) As Long
Parameters
- FileNumber
- Int32
Required. An Integer
that contains a valid file number.
Returns
A Long
specifying the current read/write position in a file opened by using the FileOpen
function, or sets the position for the next read/write operation in a file opened by using the FileOpen
function.
Exceptions
File mode is invalid.
Examples
This example uses the Seek
function to return the current file position. The example assumes TestFile
is a file that contains records of the structure Record
.
Structure Record ' Define user-defined type.
Dim ID As Integer
Dim Name As String
End Structure
For files opened in Random
mode, Seek
returns the number of next record.
FileOpen(1, "TESTFILE", OpenMode.Random)
Do While Not EOF(1)
WriteLine(1, Seek(1)) ' Write record number.
FileGet(1, MyRecord, -1) ' Read next record.
Loop
FileClose(1)
For files opened in modes other than Random
mode, Seek
returns the byte position at which the next operation occurs. Assume TestFile
is a file that contains several lines of text.
' Report character position at beginning of each line.
Dim TextLine As String
FileOpen(1, "TESTFILE", OpenMode.Input) ' Open file for reading.
While Not EOF(1)
' Read next line.
TextLine = LineInput(1)
' Position of next line.
MsgBox(Seek(1))
End While
FileClose(1)
This example uses the Seek
function to set the position for the next read or write in a file.
For files opened in modes other than Random
mode, Seek
sets the byte position at which the next operation occurs. Assume TestFile
is a file that contains several lines of text.
Dim someText As String = "This is a test string."
' Open file for output.
FileOpen(1, "TESTFILE", OpenMode.Input)
' Move to the third character.
Seek(1, 3)
Input(1, someText)
Console.WriteLine(someText)
FileClose(1)
Remarks
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 , Input , Output , Append |
Byte position at which the next operation occurs. The first byte in a file is at position 1, the second byte is at position 2, and so on. |
See also
- FileGet
- Loc(Int32)
- FileOpen(Int32, String, OpenMode, OpenAccess, OpenShare, Int32)
- FilePut
- IOException
- Reading from Files in Visual Basic
- Writing to Files in Visual Basic
Applies to
Seek(Int32, Int64)
- Source:
- FileSystem.vb
- Source:
- FileSystem.vb
- Source:
- FileSystem.vb
Returns a Long
specifying the current read/write position in a file opened by using the FileOpen
function, or sets the position for the next read/write operation in a file opened by using the FileOpen
function. The My
feature gives you better productivity and performance in file I/O operations than Seek
. For more information, see FileSystem.
public:
static void Seek(int FileNumber, long Position);
public static void Seek (int FileNumber, long Position);
static member Seek : int * int64 -> unit
Public Sub Seek (FileNumber As Integer, Position As Long)
Parameters
- FileNumber
- Int32
Required. An Integer
that contains a valid file number.
- Position
- Int64
Required. Number in the range 1-2,147,483,647, inclusive, that indicates where the next read/write operation should occur.
Exceptions
File mode is invalid.
Examples
This example uses the Seek
function to return the current file position. The example assumes TestFile
is a file that contains records of the structure Record
.
Structure Record ' Define user-defined type.
Dim ID As Integer
Dim Name As String
End Structure
For files opened in Random
mode, Seek
returns the number of next record.
FileOpen(1, "TESTFILE", OpenMode.Random)
Do While Not EOF(1)
WriteLine(1, Seek(1)) ' Write record number.
FileGet(1, MyRecord, -1) ' Read next record.
Loop
FileClose(1)
For files opened in modes other than Random
mode, Seek
returns the byte position at which the next operation occurs. Assume TestFile
is a file that contains several lines of text.
' Report character position at beginning of each line.
Dim TextLine As String
FileOpen(1, "TESTFILE", OpenMode.Input) ' Open file for reading.
While Not EOF(1)
' Read next line.
TextLine = LineInput(1)
' Position of next line.
MsgBox(Seek(1))
End While
FileClose(1)
This example uses the Seek
function to set the position for the next read or write in a file.
For files opened in modes other than Random
mode, Seek
sets the byte position at which the next operation occurs. Assume TestFile
is a file that contains several lines of text.
Dim someText As String = "This is a test string."
' Open file for output.
FileOpen(1, "TESTFILE", OpenMode.Input)
' Move to the third character.
Seek(1, 3)
Input(1, someText)
Console.WriteLine(someText)
FileClose(1)
Remarks
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 , Input , Output , Append |
Byte position at which the next operation occurs. The first byte in a file is at position 1, the second byte is at position 2, and so on. |
See also
- FileGet
- Loc(Int32)
- FileOpen(Int32, String, OpenMode, OpenAccess, OpenShare, Int32)
- FilePut
- IOException
- Reading from Files in Visual Basic
- Writing to Files in Visual Basic