目录
索引 🔎︎

# 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.

### 语法:

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

### 参数:

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.

对于二进制文件 (Binary 访问)，是文件中开始写入字节的位置。

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

相关文件的批注: 如果变量的内容与「Open」语句中的「Len」子句里面指定数据的长度不匹配的话，那么介于新写入的数据的末尾和下一条记录之间的空间，将会用你正在写入的文件中的现有数据进行填充。

二进制文件的批注: 将变量内容写入指定的位置，并将文件指针直接插入到最后一个字节后。记录之间不需要留有空间。

### 示例:

      Sub ExampleRandomAccess
          Dim iNumber As Integer
          Dim sText As Variant ' 必须是变量
          Dim aFile As String
          aFile = "C:\Users\ThisUser\data.txt"
          iNumber = Freefile
          Open aFile For Random As #iNumber Len=32
          Seek #iNumber,1 ' 起始位置
          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