Put Statement
Description
Writes from a variable to a disk file.
Syntax
Put [#]filenumber,[recnumber],varname
The Put statement syntax has these parts:
Part |
Description |
|
filenumber |
Any valid file number. |
recnumber |
Record number (Random mode files) or byte number (Binary mode files) at which writing begins. |
varname |
Name of variable containing data to be written to disk. |
Remarks
The first record/byte in a file is at position 1, the second record/byte is at position 2, and so on. If you omit recnumber, 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. You must include delimiting commas, for example:
Put #4,,FileBuffer
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 the 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 the 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 Variant of a numeric type, 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.
- Put writes elements of user-defined types as if each were written individually, except 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:
See Also
Get Statement, Open Statement, VarType Function.
Example
This example uses the Put statement to write data to a disk file. Five records of the user-defined type Record are written to the file.
' Define user-defined type.
Type Record
ID As Integer
Name As String * 20
End Type
Dim MyRecord As Record ' Declare variable.
' Open file for random access.
Open "TESTFILE" For Random As #1 Len = Len(MyRecord)
For RecordNumber = 1 To 5 ' Loop 5 times.
MyRecord.ID = RecordNumber ' Define ID.
MyRecord.Name = "My Name" & RecordNumber ' Create a string.
Put #1, RecordNumber, MyRecord ' Write record to file.
Next RecordNumber
Close #1 ' Close file.