Using Random Access Files
Random access records are stored differently from sequential access records. A random access record has a fixed length as does each field within the record. The fixed length determines the beginning and end of each record or field. Random access reads and writes a file in BINARY mode.
Two main keywords are used in random access, RECLEN and RECORD.
RECLEN statement
Purpose: RECLEN is used at the end of an OPEN file statement to calculate the fixed length of the random access record.
Syntax: OPEN fName$ FOR BINARY [NEW] AS hFile RECLEN RecordSize Parameters:
|
The program below, RndAcc.bas, is an example that calculates the size of the record. RndAcc.bas OPENS an existing text file in BINARY mode and reads random access records from the file.
'-----------Begin snip and save as RndAcc.txt-----------------0000000000:141592653589793238462643383279502884197169399375100000000001:582097494459230781640628620899862803482534211706790000000002:821480865132823066470938446095505822317253594081280000000003:481117450284102701938521105559644622948954930381960000000004:442881097566593344612847564823378678316527120190910000000005:456485669234603486104543266482133936072602491412730000000006:724587006606315588174881520920962829254091715364360000000007:789259036001133053054882046652138414695194151160940000000008:330572703657595919530921861173819326117931051185480000000009:074462379962749567351885752724891227938183011949120000000010:983367336244065664308602139494639522473719070217980000000011:609437027705392171762931767523846748184676694051320000000012:000568127145263560827785771342757789609173637178720000000013:146844090122495343014654958537105079227968925892350000000014:420199561121290219608640344181598136297747713099600000000015:518707211349999998372978049951059731732816096318590000000016:502445945534690830264252230825334468503526193118810000000017:710100031378387528865875332083814206171776691473030000000018:598253490428755468731159562863882353787593751957780000000019:18577805321712268066130019278766111959092164201989'-------------End snip and save as RndAcc.txt-----------------'RndAcc.bas Random file access of a ASCII text fileCLSDIMa$,x%,x1$,lenf%,i%,numrecs%,field%"RndAcc.txt is fixed record length including the trailing crlf."OPEN"RndAcc.txt"FORINPUTASFP1LINEINPUTFP1,a$CLOSEFP1 field%=LEN(a$)+2lenf%=LOF("RndAcc.txt")OPEN"RndAcc.txt"FORINPUTASFP1" First few lines in the file."FORi%=0TO5LINEINPUTFP1 , a$NEXTCLOSEFP1OPEN"RndAcc.txt"FORBINARYASFP1RECLENfield% numrecs%=lenf%/field%"field = ";field%;" numrecs = ";numrecs%;" Last record = ";numrecs%-1DOINPUT"Input record number to read ",x1$ x%=VAL(x1$)RECORDFP1,xGET$FP1,a$,field%LOOPUNTILx1$=""CLOSEFP1
RECORD statement
Purpose: RECORD is used to move a pointer within the file to a specific record.
Syntax: RECORD hFile, RecordNumber [,Location IN RECORD] Parameters:
|
For example,
RECORD FP1, 5
would move the pointer to the fifth record in the file.
REC function
Purpose: REC returns an integer containing the current record number.
Syntax: RetVal% = REC(hFile) Parameters:
|
LOC function
Purpose: LOC returns an integer containing the position of a pointer within the current record.
Syntax: RetVal% = LOC(hFile) Parameters:
|
Note well, that for LOC to work correctly the file must to be opened with a RECLEN , for example
OPEN "Data.bin" FOR BINARY AS FP1 RECLEN Datar RECORD FP1, 5 'move to the beginning of the 5th record x = LOC(FP1) 'x would be equal to 0 RECORD FP1, 5, 10 'move to the 10th character in the 5th record x = LOC(FP1) 'x would be equal to 10
RECCOUNT function
Purpose: RECCOUNT returns an integer containing the number of records in the random access file.
Note well that after RECCOUNT has been called, the file pointer is left at the end of the file. The REC and LOC functions could be used to determine and save the current position of the file pointer before calling RECCOUNT and then, after RECCOUNT has been called, the saved REC and LOC values could be used as arguments in a RECORD function call to move the file pointer back to the original record and position.
Syntax: RetVal% = RECCOUNT(hFile) Parameters:
|
Here is an example of writing and reading a Random Access file.
'Simple Random Access databaseDIMinpt$DIMstr1$DIMFileName$TYPEDATAREC FName[10]ASCHARMName[10]ASCHARLName[10]ASCHARAgeASINTEGERENDTYPEDIMDatarASDATARECINPUT"Filename of database to open ? ", FileName$IFEXIST(FileName$)THENOPENFileName$FORBINARYASFP1RECLENDatarELSEINPUT"File does not exist. Create?(Y/N)", str1$IFUCASE$(str1$)="Y"THENOPENFileName$FORBINARYNEWASFP1RECLENDatarELSECLOSEENDENDIFENDIFjmp1:"Press 0 to quit""Press 1 to add record""Press 2 to get record"INPUTinpt$IFinpt$="0"THENGOTOfinisENDIFIFinpt$ <>"1"ANDinpt$ <>"2"THENGOTOjmp1ENDIFIFinpt$="1"THENCALLAddRecord()ENDIFIFinpt$="2"THENCALLGetRecord()ENDIFGOTOjmp1 finis:CLOSEFP1ENDSUBAddRecord()LOCALAddRec$LOCALRecNum%DORecNum%=RECCOUNT(FP1)"Database contains";STR$(RecNum%);" records."INPUT"Add record number ", RecNum%RECORDFP1, RecNumINPUT"First Name ", Datar.FName$INPUT"Middle Name ", Datar.MName$INPUT"Last Name ", Datar.LName$INPUT"Age ", Datar.Age%PUT$FP1,&Datar,SIZEOF(Datar)INPUT"Add another record(Y/N)? ", AddRec$ RecNum%++LOOPUNTILUCASE$(AddRec$)="N"ENDSUBSUBGetRecord()LOCALGetRec%LOCALRecNum%DOINPUT"Get record number(0 to quit) ", GetRec%IFGetRec%=0THENEXITLOOPENDIFRECORDFP1, GetRecGET$FP1,&Datar,SIZEOF(Datar)LOOPENDSUB