Loc Function
Returns a Long value specifying the current read/write position within an open file.
Public Function Loc(ByVal FileNumber As Integer) As Long
Exception type | Error number | Condition |
|---|---|---|
FileNumber does not exist. | ||
File mode is invalid. |
See the "Error number" column if you are upgrading Visual Basic 6.0 applications that use unstructured error handling. (You can compare the error number against the Number Property (Err Object).) However, when possible, you should consider replacing such error control with Structured Exception Handling Overview for Visual Basic.
The Loc function is zero-based; using it to get the first byte in a file will return 0.
The Loc function is provided for backward compatibility and may have an impact on performance. For non-legacy applications, the My.Computer.FileSystem object provides better performance. For more information, see File Access with Visual Basic.
The following describes the return value for each file access mode:
Mode | Return value |
|---|---|
Random | Number of the last record read from or written to the file. |
Sequential | Current byte position in the file divided by 128. However, information returned by Loc for sequential files is neither used nor required. |
Binary | Position of the last byte read or written. |
This example uses the Loc function to return the current read/write position within an open file. This example assumes that MyFile is a text file with a few lines of sample data.
Dim location As Long Dim oneChar As Char FileOpen(1, "C:\TESTFILE.TXT", OpenMode.Binary) While location < LOF(1) Input(1, oneChar) location = Loc(1) WriteLine(1, location & ControlChars.CrLf) End While FileClose(1)
Namespace: Microsoft.VisualBasic
Module: FileSystem
Assembly: Visual Basic Runtime Library (in Microsoft.VisualBasic.dll)