PUT: Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
m (Removed protection from "PUT")
No edit summary
Line 8: Line 8:




* File/port number is the number used in the [[OPEN]] statement.  
* File/port number 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 [[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 file byte or record ''position'' can be omitted if the [[PUT]] or [[GET]] is consecutive or when creating new file data sequentially.
* The ''holding variable'' [[TYPE|type]] determines byte size and the next byte position in the file when the ''position'' is ommitted.
* The ''holding variable'' [[TYPE|type]] determines byte size and the next byte position in the file when the ''position'' is ommitted.
* The first byte or record position is 1. This may require adding one to an offset value when documentation uses that position as 0.
* The first byte or record position is 1. This may require adding one to an offset value when documentation uses that position as 0.
Line 21: Line 21:


''Example 1:'' Using a [[TYPE]] record variable(Contact) to enter a new [[RANDOM]] record to a file.
''Example 1:'' Using a [[TYPE]] record variable(Contact) to enter a new [[RANDOM]] record to a file.
{{CodeStart}} '' ''
{{CodeStart}}
{{Cl|TYPE}} ContactType
{{Cl|TYPE}} ContactType
   first {{Cl|AS}} {{Cl|STRING}} * 10
   first {{Cl|AS}} {{Cl|STRING}} * 10
Line 38: Line 38:


{{Cl|PUT}} #1, NumRecords% + 1, Contact ' add a new record {{Cl|TYPE}} record value
{{Cl|PUT}} #1, NumRecords% + 1, Contact ' add a new record {{Cl|TYPE}} record value
{{Cl|CLOSE}} #1 '' ''
{{Cl|CLOSE}} #1
{{CodeEnd}}
{{CodeEnd}}
: ''Note:'' The DOT record variable values were created or changed before the PUT. The record length is 32 bytes.
: ''Note:'' The DOT record variable values were created or changed before the PUT. The record length is 32 bytes.
Line 44: Line 44:


''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.
''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.
{{CodeStart}} '' ''
{{CodeStart}}
{{Cl|DIM}} {{Cl|SHARED}} array(100) {{Cl|AS}} {{Cl|INTEGER}}
{{Cl|DIM}} {{Cl|SHARED}} array(100) {{Cl|AS}} {{Cl|INTEGER}}


Line 72: Line 72:
{{Cl|NEXT}}
{{Cl|NEXT}}
{{Cl|PRINT}} "done"
{{Cl|PRINT}} "done"
{{Cl|END SUB}} '' ''
{{Cl|END SUB}}
{{CodeEnd}}
{{CodeEnd}}
: ''Note:'' Use empty brackets in QB64 when using [[GET]] to create an array or [[PUT]] to create a [[BINARY]] data file.
: ''Note:'' Use empty brackets in QB64 when using [[GET]] to create an array or [[PUT]] to create a [[BINARY]] data file.
Line 80: Line 80:




''See also:''  
''See also:''
* [[GET|GET #]]  
* [[GET|GET #]]
* [[SEEK]], [[SEEK (statement)]]  
* [[SEEK]], [[SEEK (statement)]]
* [[PRINT (file statement)|PRINT  #]]  
* [[PRINT (file statement)|PRINT  #]]
* [[FIELD]]  
* [[FIELD]]
* [[PUT (graphics statement)]]
* [[PUT (graphics statement)]]
* [[PUT (TCP/IP statement)]]
* [[PUT (TCP/IP statement)]]

Revision as of 02:24, 23 January 2023

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


Syntax

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


  • File/port number 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 holding variable type determines byte size and the next byte position in the file when the position is ommitted.
  • 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 holding variable(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 holding variable, 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()


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
Note: The DOT record variable values were created or changed before the PUT. The record length is 32 bytes.


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
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: Program ScreenShots


See also:



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