mirror of
https://github.com/QB64Official/qb64.git
synced 2024-09-28 11:17:47 +00:00
105 lines
4.2 KiB
Text
105 lines
4.2 KiB
Text
'''RANDOM''' is used in an [[OPEN]] statement to read([[GET]]) from or write([[PUT]]) to a file.
|
|
|
|
|
|
|
|
{{PageSyntax}}
|
|
:: OPEN Filename$ FOR RANDOM AS #1 [LEN = ''recordlength%'']
|
|
|
|
|
|
* RANDOM is the Default mode if no mode is given in the [[OPEN]] statement.
|
|
* It creates the file if the legal file name given does NOT exist.
|
|
* As a RANDOM file, it can read or write any record using [[GET]] and/or [[PUT]] statements.
|
|
* ''Recordlength%'' is determined by getting the LEN of a [[TYPE]] variable or a [[FIELD]] statement.
|
|
:: [[STRING]] = 1 byte/character, [[INTEGER]] = 2 bytes, [[LONG]] = 4 bytes, [[SINGLE]] = 4 bytes [[DOUBLE]] = 8 bytes
|
|
:: [[_BYTE]] = 1 byte, [[_INTEGER64]] = 8 bytes, [[_FLOAT]] = 10 bytes (so far)
|
|
|
|
* If no record length is used in the [[OPEN]] statement, the default record size is 128 bytes except for the last record.
|
|
* A record length cannot exceed 32767 or an [[ERROR Codes|error]] will occur!
|
|
* To determine the number of records in a file the records% = [[LOF]] \ recordlength%.
|
|
* When '''variable length strings''' are PUT into RANDOM files the record length must exceed the maximum string entry by:
|
|
:: 2 bytes are reserved for recording variable string lengths up to 32767 bytes (LEN = longest + 2)
|
|
:: 8 bytes are reserved for recording variable string lengths exceeding 32767 bytes (LEN = longest + 8)
|
|
* A serial communication port can also be opened for RANDOM in an [[OPEN COM]] statement.
|
|
|
|
|
|
''Example 1:'' Function that finds a RANDOM file's record number for a string value such as a phone number.
|
|
{{CodeStart}} '' ''
|
|
{{Cl|TYPE}} customer
|
|
age {{Cl|AS}} {{Cl|INTEGER}}
|
|
phone {{Cl|AS}} {{Cl|STRING}} * 10
|
|
{{Cl|END}} {{Cl|TYPE}}
|
|
|
|
{{Cl|DIM}} {{Cl|SHARED}} cust {{Cl|AS}} customer, recLEN
|
|
recLEN = {{Cl|LEN}}(cust) 'get the length of the record type
|
|
{{Cl|PRINT}} "Rec{{Cl|LEN}}:"; recLEN
|
|
|
|
{{Cl|OPEN}} "randfile.rec" {{Cl|FOR...NEXT|FOR}} {{Cl|RANDOM}} {{Cl|AS}} #1 {{Cl|LEN}} = recLEN
|
|
{{Cl|FOR...NEXT|FOR}} i = 1 {{Cl|TO}} 4
|
|
{{Cl|READ}} cust.age, cust.phone
|
|
{{Cl|PUT}} #1, , cust
|
|
{{Cl|NEXT}}
|
|
{{Cl|CLOSE}} #1
|
|
|
|
RP = RecordPos("randfile.rec", "2223456789") 'returns 0 if record not found!
|
|
|
|
{{Cl|PRINT}} RP
|
|
|
|
{{Cl|IF...THEN|IF}} RP {{Cl|THEN}}
|
|
{{Cl|OPEN}} "randfile.rec" {{Cl|FOR...NEXT|FOR}} {{Cl|RANDOM}} {{Cl|AS}} #2 {{Cl|LEN}} = recLEN
|
|
{{Cl|GET}} #2, RP, cust
|
|
{{Cl|CLOSE}} #2
|
|
{{Cl|PRINT}} cust.age, cust.phone
|
|
{{Cl|END IF}}
|
|
|
|
{{Cl|END}}
|
|
|
|
{{Cl|DATA}} 59,2223456789,62,4122776477,32,3335551212,49,1234567890
|
|
|
|
{{Cl|FUNCTION}} RecordPos (file$, search$)
|
|
f = {{Cl|FREEFILE}}
|
|
{{Cl|OPEN}} file$ {{Cl|FOR (file statement)|FOR}} {{Cl|INPUT (file mode)|INPUT}} {{Cl|AS}} #f
|
|
FL = {{Cl|LOF}}(f)
|
|
dat$ = {{Cl|INPUT$}}(FL, f)
|
|
{{Cl|CLOSE}} f
|
|
recpos = {{Cl|INSTR}}(dat$, search$)
|
|
{{Cl|IF...THEN|IF}} recpos {{Cl|THEN}} RecordPos = recpos \ recLEN + 1 {{Cl|ELSE}} RecordPos = 0
|
|
{{Cl|END FUNCTION}} '' ''
|
|
{{CodeEnd}}
|
|
:''Note:'' Random files can store records holding various variable types using a [[TYPE]] definition or a [[FIELD]] statement.
|
|
|
|
|
|
''Example 2:'' When not using a [[TYPE]] or fixed length strings, QB4.5 allows RANDOM files to hold variable length strings up to 2 bytes less than the LEN = record length statement:
|
|
{{CodeStart}} '' ''
|
|
{{Cl|_CONTROLCHR}} OFF
|
|
{{Cl|OPEN}} "myfile.txt" {{Cl|FOR...NEXT|FOR}} {{Cl|OUTPUT}} {{Cl|AS}} #1: {{Cl|CLOSE}} #1: ' clears former file of all entries.
|
|
{{Cl|OPEN}} "myfile.txt" {{Cl|FOR...NEXT|FOR}} {{Cl|RANDOM}} {{Cl|AS}} #1 {{Cl|LEN}} = 13 'strings can be up to 11 bytes with 2 byte padder
|
|
|
|
a$ = {{Cl|CHR$}}(1) + {{Cl|CHR$}}(0) + "ABCDEFGHI"
|
|
b$ = "ABCDEFGHI"
|
|
c$ = "1234"
|
|
|
|
{{Cl|PUT}} #1, 1, a$
|
|
{{Cl|PUT}} #1, 2, b$
|
|
{{Cl|PUT}} #1, 3, c$
|
|
|
|
{{Cl|FOR...NEXT|FOR}} i = 1 {{Cl|TO}} 3
|
|
{{Cl|GET}} #1, i, a$
|
|
{{Cl|PRINT}} a$, {{Cl|LEN}}(a$)
|
|
{{Cl|NEXT}}
|
|
|
|
{{Cl|CLOSE}} '' ''
|
|
{{CodeEnd}}
|
|
{{OutputStart}}☺ ABCDEFGHI 11
|
|
ABCDEFGHI 9
|
|
1234 4
|
|
{{OutputEnd}}
|
|
: ''Note:'' The 2 byte file padders before each string PUT will show the length of a string for GET as [[ASCII]] characters. Padders will always be 2 bytes and strings up to the last one will be 13 bytes each no matter the length up to 11, so the file size can be determined as (2 + 11) + (2 + 9 + 2) + (2 + 4) or 13 + 13 + 2 + 4 = 32 bytes.
|
|
|
|
|
|
''See also:''
|
|
* [[GET]], [[PUT]], [[FIELD]]
|
|
* [[BINARY]]
|
|
* [[SEEK]], [[SEEK (statement)]]
|
|
|
|
|
|
{{PageNavigation}}
|