Contents

# Eof Function

確定檔案指標是否到達檔案的結尾。

### Syntax:

    Eof (intexpression As Integer)

### Return type:

布林型

### Parameters:

Intexpression：任意整型表示式，用於演算開啟著的檔案之編號。

使用 EOF 函式以避免嘗試超過檔案結尾輸入的錯誤。當您使用 Input 或 Get 陳述式讀取檔案時，檔案指標按照所讀取的位元組數向前移動。到達檔案結尾時，EOF 函式將傳回「True (真)」(-1)。

### 錯誤代碼：

5 無效的程序呼叫

52 錯誤的檔案名稱或檔案編號

### Example:

      Sub ExampleWorkWithAFile
          Dim iNumber As Integer
          Dim sLine As String
          Dim aFile As String
          Dim sMsg As String
          aFile = "C:\Users\ThisUser\data.txt"
          iNumber = Freefile
          Open aFile For Output As #iNumber
          Print #iNumber, "This is a line of text"
          Print #iNumber, "This is another line of text"
          Close #iNumber
          iNumber = Freefile
          Open aFile For Input As iNumber
          While Not eof(iNumber)
              Line Input #iNumber, sLine
              If sLine <>"" Then
                  sMsg = sMsg & sLine & chr(13)
              End If
          Wend
          Close #iNumber
          MsgBox sMsg
      End Sub

      Sub ExampleWorkWithAFile
          Dim iNumber As Integer
          Dim sLine As String
          Dim aFile As String
          Dim sMsg As String
          aFile = "~/data.txt"
          iNumber = Freefile
          Open aFile For Output As #iNumber
          Print #iNumber, "This is a line of text"
          Print #iNumber, "This is another line of text"
          Close #iNumber
          iNumber = Freefile
          Open aFile For Input As iNumber
          While Not eof(iNumber)
              Line Input #iNumber, sLine
              If sLine <>"" Then
                  sMsg = sMsg & sLine & chr(13)
              End If
          Wend
          Close #iNumber
          MsgBox sMsg
      End Sub

相關主題

Close Statement

Print# Statement

Input# Statement

Open Statement

Please support us!