QB64.com

QB64 is a modern extended BASIC programming language that retains QBasic/QuickBASIC 4.5 compatibility and compiles native binaries for Windows, Linux, and macOS.

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

Syntax

PUT #filenumber&, [position][, {holdingvariable holdingarray()}]

Example(s)

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 

Note: The DOT record variable values were created or changed before the PUT. The record length is 32 bytes.

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
showme  'display array contents

OPEN "BINFILE.BIN" FOR BINARY AS #1

PUT #1, , array()

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

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

END

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

Note: Use empty brackets in QB64 when using GET to create an array or PUT to create a BINARY data file.

See Example(s)

See Also