See Also Example
Writes from a variable to a disk file.
Syntax
Put [#] filenumber, [recordnumber], variablename
Remarks
The Put statement has these parts:
Part Description
filenumber Number used in an Open statement to open the file. It can be any numeric expression that evaluates to the number of an open file.
recordnumber The largest possible recordnumber is 2^31 - 1, or 2,147,483,647. For files opened in Random mode (with the Random reserved word ), recordnumber is the number of the record to be written. For files opened in Binary mode (with the Binary reserved word), recordnumber is the byte position at which writing starts. The first byte in a file is at position 1, the second byte is at position 2, and so on. If you omit recordnumber, the next record or byte (the one after the last Get or Put statement or the one pointed to by the last Seek function) is written. And if you omit recordnumber, you must still include the delimiting commas. For example:
Put #4,, FileBuffer
variablename Name of the variable to write to the file. Any variable can be used except object variables and array variables (variables that describe an entire array). However, you can use a variable that describes a single element of an array.
For files opened in Random mode, the following rules apply:
If the length of the data being written is less than the length specified in the Len clause of the Open statement, Put still writes subsequent records on record-length boundaries. The space between the end of one record and the beginning of the next record is padded with the existing contents of the file buffer. Because the amount of padding data can't be determined with any certainty, it is generally a good idea to have record length match the length of the data being written.
If the variable being written is a variable-length string, Put writes a 2-byte descriptor containing string length and then the variable. The record length specified by the Len clause in the Open statement must be at least 2 bytes greater than the actual length of the string.
If the variable being written is a numeric Variant (VarType 0-7), Put writes 2 bytes identifying the VarType of the Variant and then the variable. For example, when writing a Variant of VarType 3, Put writes 6 bytes: 2 bytes identifying the Variant as VarType 3 (Long) and 4 bytes containing the Long data. The record length specified by the Len clause in the Open statement must be at least 2 bytes greater than the actual number of bytes required to store the variable.
If the variable being written is a String Variant (VarType 8), Put writes 2 bytes identifying the VarType, 2 bytes indicating the length of the string, and then the string data. The record length specified by the Len clause in the Open statement must be at least 4 bytes greater than the actual length of the string.
If the variable being written is any other type of variable (not a variable-length string and not a Variant), Put writes only the variable data. The record length specified by the Len clause in the Open statement must be greater than or equal to the length of the data being written.
Elements of user-defined types are written using Put as if each were written individually except that there is no padding between elements. The record length specified by the Len clause in the Open statement must be greater than or equal to the sum of all the bytes required to write the individual elements.
For files opened in Binary mode, all of the Random rules apply except that:
In Binary mode, the Len clause in the Open statement has no effect. Put writes all variables to disk contiguously, that is, with no padding between records.
Put writes variable-length strings that are not elements of user-defined types without the 2-byte length descriptor. The number of bytes written equals the number of characters in the string. For example, the following statements write 10 bytes to file number 1:
VarString$ = String$(10," ")
Put #1,,VarString$
Get Statement
Open Statement
The example uses Put to write data to a disk file. To try this example, paste the code into the Declarations section of a form. Then press F5 and click the form.
Sub Form_Click ()
Dim ClientName As String * 20 ' Declare variables.
Dim Indx, Max, Msg, NL
NL = Chr(10) ' Define newline.
' Make a sample random-access file using the Put statement.
Msg = "Enter a name, using whatever form you like." & NL & NL
Msg = Msg & "Press 'Enter' with no name to conclude entry."
Open "TESTFILE" For Random As #1 Len = Len(ClientName)
ClientName = InputBox(Msg) ' Get user input.
Do While ClientName <> String(20, " ")
Put #1, , ClientName ' Write to file.
ClientName = InputBox(Msg) ' Get user input.
Loop
' Read the sample random-access file using the Get statement.
Max = Loc(1)
If Max = 0 Then ' Check if records exist.
Msg = "Your file contains no names." & NL
Else
Msg = "Your file contains the following names:" & NL & NL
End If
For Indx = 1 To Max
Get #1, Indx, ClientName ' Read from file.
Msg = Msg & ClientName & NL
Next Indx
Close #1 ' Close file.
Msg = Msg & NL & "Choose OK to remove the created test file."
MsgBox Msg ' Display message.
Kill "TESTFILE" ' Remove file from disk.
End Sub