FileSystem.Seek 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
回傳 Long 使用 該 FileOpen 函式開啟檔案時,指定當前讀寫位置,或設定使用該函式開啟 FileOpen 檔案下一次讀寫操作的位置。 這個 My 功能能讓你在檔案 I/O 操作 Seek中提升生產力和效能。 如需詳細資訊,請參閱FileSystem。
多載
| 名稱 | Description |
|---|---|
| Seek(Int32) |
回傳 |
| Seek(Int32, Int64) |
回傳 |
Seek(Int32)
回傳 Long 使用 該 FileOpen 函式開啟檔案時,指定當前讀寫位置,或設定使用該函式開啟 FileOpen 檔案下一次讀寫操作的位置。 這個 My 功能能讓你在檔案 I/O 操作 Seek中提升生產力和效能。 如需詳細資訊,請參閱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
參數
- FileNumber
- Int32
必須的。 一個 Integer 包含有效檔案號的 that。
傳回
A Long 指定使用 FileOpen 函式開啟檔案時的當前讀寫位置,或設定使用函式開啟 FileOpen 檔案中下一次讀寫操作的位置。
例外狀況
檔案模式無效。
範例
此範例使用 Seek 回傳當前檔案位置的函式。 範例假設 TestFile 是一個包含結構 Record記錄的檔案。
Structure Record ' Define user-defined type.
Dim ID As Integer
Dim Name As String
End Structure
對於在 Random 模式中開啟的檔案,會 Seek 回傳下一個記錄的編號。
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)
對於在非 Random 模式模式開啟的檔案,會 Seek 回傳下一個操作發生的位元組位置。
TestFile假設是一個包含多行文字的檔案。
' 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)
這個範例使用 Seek 了設定檔案下一次讀取或寫入位置的函式。
對於以非 Random 模式模式開啟的檔案,會 Seek 設定下一個操作發生的位元組位置。
TestFile假設是一個包含多行文字的檔案。
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)
備註
Seek 回傳的值介於 1 到 2,147,483,647(相當於 2^31 - 1),包含。
以下說明每種檔案存取模式的回傳值:
| 模式 | 傳回值 |
|---|---|
Random |
下一條記錄的讀取或寫入數量 |
Binary、Input、Output、Append |
下一個操作發生的位元組位置。 檔案中的第一個位元組位於位置 1,第二個位元組位於位置 2,依此類推。 |
另請參閱
- FileGet
- Loc(Int32)
- FileOpen(Int32, String, OpenMode, OpenAccess, OpenShare, Int32)
- FilePut
- IOException
讀取 Visual Basic 寫入 Visual Basic
適用於
Seek(Int32, Int64)
回傳 Long 使用 該 FileOpen 函式開啟檔案時,指定當前讀寫位置,或設定使用該函式開啟 FileOpen 檔案下一次讀寫操作的位置。 這個 My 功能能讓你在檔案 I/O 操作 Seek中提升生產力和效能。 如需詳細資訊,請參閱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)
參數
- FileNumber
- Int32
必須的。 一個 Integer 包含有效檔案號的 that。
- Position
- Int64
必須的。 包含 1-2,147,483,647 的數字,表示下一次讀寫操作應在哪裡進行。
例外狀況
檔案模式無效。
範例
此範例使用 Seek 回傳當前檔案位置的函式。 範例假設 TestFile 是一個包含結構 Record記錄的檔案。
Structure Record ' Define user-defined type.
Dim ID As Integer
Dim Name As String
End Structure
對於在 Random 模式中開啟的檔案,會 Seek 回傳下一個記錄的編號。
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)
對於在非 Random 模式模式開啟的檔案,會 Seek 回傳下一個操作發生的位元組位置。
TestFile假設是一個包含多行文字的檔案。
' 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)
這個範例使用 Seek 了設定檔案下一次讀取或寫入位置的函式。
對於以非 Random 模式模式開啟的檔案,會 Seek 設定下一個操作發生的位元組位置。
TestFile假設是一個包含多行文字的檔案。
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)
備註
Seek 回傳的值介於 1 到 2,147,483,647(相當於 2^31 - 1),包含。
以下說明每種檔案存取模式的回傳值:
| 模式 | 傳回值 |
|---|---|
Random |
下一條記錄的讀取或寫入數量 |
Binary、Input、Output、Append |
下一個操作發生的位元組位置。 檔案中的第一個位元組位於位置 1,第二個位元組位於位置 2,依此類推。 |
另請參閱
- FileGet
- Loc(Int32)
- FileOpen(Int32, String, OpenMode, OpenAccess, OpenShare, Int32)
- FilePut
- IOException
讀取 Visual Basic 寫入 Visual Basic