Put Statement
Writes data from a variable to a disk file.
Syntax
Put [#]filenumber, [recnumber], varname |
The Put statement syntax has the following parts:
Part |
Description |
filenumber |
Required. Any valid file number. |
recnumber |
Optional. Record number (Random mode files)
or byte number (Binary mode files) at which writing begins. |
varname |
Required. Name of variable containing data to
be written to disk. |
Remarks
Data written with Put is usually read from
a file with Get.
The first record or byte in a file is at position 1, the second record or file
is at position 2 and so on. If you omit recnumber, the next record or
byte after the last Get or Put
statement or pointed to by the 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 lenght specified
in the Len clause of the Open statement, Put 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 padded data can't be determined
with any certainty, it is generally a good idea to have the record lenght
match the length of the data being written. If the length of the data being
written is greater than the lenght specified in the Len clause in the
Open statement, an error occurs.
- If the variable being written is a variable-lenght string, Put writes
a 2-byte descriptor containing the string lenght, and then the variable. The
record lenght specified in the Len clause in the Open statement
must be at least 2 bytes greater than the actual lenght of the string.
- If the variable being written is a Variant of a numeric type, Put
writes 2 bytes identifuing the VarType of the Variant, and then
writes the variable. For example, when writing a Variant VarType
3, Put writes 6 bytes: 2 bytes identyfying the Variant as VarType
3 (Long), and 4 bytes containing the Long data. The record lenght
specified in 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.
Example
In this example the Put statement is used to write data to a file.
Dim sName as String*20, nRecordNumber ' Declares variable.
' Opens file for Random access. Open "TESTFILE" For Random As #1 Len = 21
For nRecordNumber = 1 To 5 ' Repeats the loop 5 times. sName = "My Name " & nRecordNumber ' Creates a string. Put #1, nRecordNumber, sName ' Writes record to file. Next nRecordNumber
Close #1 ' Closes file.
|