Contents

# Put# Statement

將資料條目寫入相對檔案中，或將位元組序列寫入二進制檔案中。

Use Print# statement to print data to a sequential text file. Use Write# statement to write data to a sequential text file with delimiting characters.

### Syntax:

    Put [#]fileNum, [recordNum|filePos], variable

### Parameters:

fileNum: Any integer expression that defines the file that you want to write to.

recordNum, filePos: For relative files (random access files), the number of the record that you want to write.

對於二進制檔案 (二進制存取)，是指檔案中開始寫入的位元組位置。

variable: Name of the variable that you want to write to the file.

相對檔案的備註：如果此變數的內容不符合 Open 陳述式的 Len 子句中指定的資料條目之長度，則新寫入的資料條目與下一個資料條目之間的空間會由目標檔案中的現有資料所填補。

二進制檔案的備註：變數的內容會寫入至指定的位置，而檔案指標會直接插入到最後一個位元組後。資料條目之間不留空間。

### Example:

      Sub ExampleRandomAccess
          Dim iNumber As Integer
          Dim sText As Variant REM 必須是變體型變數
          Dim aFile As String
          aFile = "C:\Users\ThisUser\data.txt"
          iNumber = Freefile
          Open aFile For Random As #iNumber Len=32
          Seek #iNumber,1 REM 指定開始位置
          Put #iNumber, , "This is the first line of text" ' Fill line with text
          Put #iNumber, , "This is the second line of text"
          Put #iNumber, , "This is the third line of text"
          Seek #iNumber,2
          Get #iNumber, , sText
          Print sText
          Close #iNumber
          iNumber = Freefile
          Open aFile For Random As #iNumber Len=32
          Get #iNumber, 2, sText
          Put #iNumber, , "This is a new text"
          Get #iNumber, 1, sText
          Get #iNumber, 2, sText
          Put #iNumber, 20, "This is the text in record 20"
          Print Lof(#iNumber)
          Close #iNumber
      End Sub

      Sub ExampleRandomAccess
          Dim iNumber As Integer
          Dim sText As Variant ' Must be a variant
          Dim aFile As String
          aFile = "~/data.txt"
          iNumber = Freefile
          Open aFile For Random As #iNumber Len=32
          Seek #iNumber,1 ' Position at beginning
          Put #iNumber, , "This is the first line of text" ' Fill line with text
          Put #iNumber, , "This is the second line of text"
          Put #iNumber, , "This is the third line of text"
          Seek #iNumber,2
          Get #iNumber, , sText
          Print sText
          Close #iNumber
          iNumber = Freefile
          Open aFile For Random As #iNumber Len=32
          Get #iNumber, 2, sText
          Put #iNumber, , "This is a new text"
          Get #iNumber, 1, sText
          Get #iNumber, 2, sText
          Put #iNumber, 20, "This is the text in record 20"
          Print Lof(#iNumber)
          Close #iNumber
      End Sub

相關主題

Close Statement

Get Statement

Open Statement