PUT

From QB64 Phoenix Edition Wiki
Revision as of 17:42, 23 April 2026 by RhoSigma (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

The PUT # file or port statement writes data to a specific byte or record position.


Syntax

PUT #fileNumber&, [position][, {holdingVariable|holdingArray()}]


Description

  • fileNumber& is the number used in the OPEN statement.
  • The INTEGER or LONG file byte position in a BINARY file or the record position in a RANDOM file must be greater than zero.
  • The file byte or record position can be omitted if the PUT or GET is consecutive or when creating new file data sequentially.
  • The holdingVariable TYPE determines byte size and the next byte position in the file when the position is ommitted.
    • PUT # can handle fixed size TYPEs only, if a given TYPE contains variable length STRING elements, then you get a syntax error.
  • The first byte or record position is 1. This may require adding one to an offset value when documentation uses that position as 0.
  • Both the file position and holdingVariable(and comma) can be omitted when using a FIELD definition.
  • If a LEN = record length statement is omitted in an OPEN FOR RANDOM statement the record size defaults to 128 bytes!
  • Warning: Not designating a PUT position can overwrite previous file data based on the current file position!
  • When using a numeric holdingVariable, values do NOT require conversion using MKI$, MKL$, MKS$ or MKD$.
  • QB64 can load array data directly(brackets required) to a BINARY file using one PUT to a BINARY file: PUT #1, , array()


Availability

  • Since arrays in TYPEs were implemented in QB64-PE v4.5.0, this statement was adapted to properly handle it (see Example 3).


Examples

Example 1
Using a TYPE record variable(Contact) to enter a new RANDOM record to a file.
TYPE ContactType
    first AS STRING * 10
    last AS STRING * 20
    age AS INTEGER
END TYPE
DIM Contact AS ContactType

INPUT "Enter a first name: ", Contact.first
INPUT "Enter a last name: ", Contact.last
INPUT "Enter an age: ", Contact.age

OPEN "Record.lst" FOR RANDOM AS #1 LEN = LEN(Contact)
NumRecords% = LOF(1) \ LEN(Contact)
PRINT NumRecords%; "previous records"

PUT #1, NumRecords% + 1, Contact ' add a new record TYPE record value
CLOSE #1

Example 2
Placing the contents of a numerical array into a BINARY file. You may want to put the array size at the beginning too.
DIM SHARED array(100) AS INTEGER

FOR i = 1 TO 100
    array(i) = i
NEXT
PrintArray 'display array contents

OPEN "BINFILE.BIN" FOR BINARY AS #1

PUT #1, , array()

ERASE array 'clear element values from array and display empty
PrintArray
CLOSE #1

OPEN "BINFILE.BIN" FOR BINARY AS #2
GET #2, , array()
CLOSE #2
PrintArray 'display array after transfer from file

END

SUB PrintArray
    FOR i = 1 TO 100
        PRINT array(i);
    NEXT
    PRINT "done"
    PRINT
END SUB

Example 3
Placing the contents of a nested numerical array into a BINARY file. You may want to put the array size at the beginning too.
TYPE test
    aaa(2) AS DOUBLE
END TYPE

DIM a(1) AS test
PRINT "Generating values..."
FOR y = 0 TO 1
    FOR x = 0 TO 2
        a(y).aaa(x) = (x + 1) * (y + 1)
        PRINT (x + 1) * (y + 1);
NEXT x, y
PRINT
OPEN "s_test.dat" FOR BINARY AS #1
PRINT
PRINT "Saving nested array content to file s_test.dat"
PUT #1, , a()
CLOSE #1

ERASE a 'delete content for a() and all nested arrays in a (see ERASE for more info)

PRINT
PRINT "Reading saved content..."
OPEN "s_test.dat" FOR BINARY AS #1
GET #1, , a()
CLOSE #1

FOR y = 0 TO 1
    FOR x = 0 TO 2
        PRINT a(y).aaa(x);
    NEXT x
NEXT y


See also



Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link