Also how I can emulate the OPEN...FOR RANDOM in other Basic.
Example:
Code: Select all
TYPE EMPLOYES
CODE AS STRING * 10
NAME AS STRING * 60
END TYPE
GLOBAL EMPLOYESX AS EMPLOYES
OPEN "FILE.DAT" FOR RANDOM AS #1 LEN=70
GET #1,1,EMPLOYESX
Code: Select all
TYPE EMPLOYES
CODE AS STRING * 10
NAME AS STRING * 60
END TYPE
GLOBAL EMPLOYESX AS EMPLOYES
OPEN "FILE.DAT" FOR RANDOM AS #1 LEN=70
GET #1,1,EMPLOYESX
Code: Select all
RunProgram("cmd.exe")
Code: Select all
Structure EMPLOYEES
code.s{10}
name.s{60}
EndStructure
Declare UpdateEmployeeRecord(file, record_number, *record.EMPLOYEES)
Declare ReadEmployeeRecord(file, record_number)
With employee.EMPLOYEES
\code = "1234"
\name = "Barry Brigley"
EndWith
If CreateFile(0, "c:\employees.dat")
For i=1 To 10*SizeOf(EMPLOYEES) ; We'll make the 10 empty records
WriteByte(0, 0)
Next
CloseFile(0)
EndIf
If OpenFile(0, "c:\employees.dat")
If UpdateEmployeeRecord(0, 4, @employee) ; Store a test record in slot 4
Debug "Record Successfully Stored"
EndIf
CloseFile(0)
EndIf
If ReadFile(0, "c:\employees.dat")
*record.EMPLOYEES = ReadEmployeeRecord(0, 4) ; Read our test record back
If *record
Debug *record\code
Debug *record\name
FreeMemory(*record)
EndIf
CloseFile(0)
EndIf
End
Procedure ReadEmployeeRecord(file, record_number)
*result.EMPLOYEES = AllocateMemory(SizeOf(EMPLOYEES))
FileSeek(file, record_number * SizeOf(EMPLOYEES))
If ReadData(file, *result, SizeOf(EMPLOYEES)) = SizeOf(EMPLOYEES)
ProcedureReturn *result
Else
ProcedureReturn 0
EndIf
EndProcedure
Procedure UpdateEmployeeRecord(file, record_number, *record.EMPLOYEES)
FileSeek(file, record_number * SizeOf(EMPLOYEES))
If WriteData(file, @*record\code, SizeOf(employees\code)) = SizeOf(employees\code) And
WriteData(file, @*record\name, SizeOf(employees\name)) = SizeOf(employees\name)
ProcedureReturn 1
Else
ProcedureReturn 0
EndIf
EndProcedure