11-17-2024, 05:01 PM
Hi
I liked to post a my 2cent demo on UDT and Random File for database...
it follows the old fashion of GOSUB and GOTO but each GOSUB / RETURN can be switched into a SUB/END SUB with the right parameters.
While ON ERROR GOTO cannot be converted into modern Subroutine.
It shows the difference between Qbasic/QB45 and QB64pe: the news is that you can save and read a whole array to/from file in QB64pe.
I liked to post a my 2cent demo on UDT and Random File for database...
Code: (Select All)
Rem demo of UDT database
Rem #defining UDT, #array of UDT, #creating reading overwriting RANDOM file
Rem #reading array in one shot, #managing file, #handle runtime error
Rem #using Global subroutines, #console mode output, #formatting output text
Type MyRecord
i As Integer
s As Single
sS As String * 10
leng As Long
End Type
Dim MioRecord(1 To 9) As MyRecord
Cls , 1
GoSub MakeDataFile
Cls , 2
GoSub ReadDataFile
Cls , 3
GoSub ShowDataOnRequest
Cls , 1
Print " Quitting"
End
PrintDataOnScreen:
Cls , 4
Print " Record number " + ss$
Print
Print "Miorecord.i = "; MioRecord(Val(ss$)).i
Print "Miorecord.s = "; MioRecord(Val(ss$)).s
Print "Miorecord.sS = "; MioRecord(Val(ss$)).sS
Print "Miorecord.leng = "; MioRecord(Val(ss$)).leng
Print
GoSub WaitAKeyPressed
Return
ShowDataOnRequest:
ss$ = ""
While ss$ <> "Q"
Locate 1, 1
Print "Press 1 to 9 for see data in choosen record"
ss$ = UCase$(InKey$)
If InStr("123456789", ss$) > 0 And ss$ <> "" Then GoSub PrintDataOnScreen
' remember to avoid to search a null string using INSTR otherwise you'll get a logical bug
Wend
Return
ReadDataFile:
If _FileExists(NomeFile$) Then Print "File already on disc, we read it!" Else Print "File not found, operation failed!"
On Error GoTo Failure
numfile = FreeFile
Open NomeFile$ For Random As #numfile Len = Len(MioRecord())
Get #numfile, , MioRecord() ' <-- one step to get data of an array!The old way need a FOR or WHILE NOT EOF() loop
Close #numfile
Print "Read data from random file"
GoSub WaitAKeyPressed
Return
MakeDataFile:
NomeFile$ = "DummyDat.dat"
If _FileExists(NomeFile$) Then Print "File already on disc, we overwrite it!" Else Print "File not found, we create it!"
numfile = FreeFile
Open NomeFile$ For Random As #numfile Len = Len(MioRecord())
For a% = 1 To 9 Step 1
MioRecord(a%).i = a%
MioRecord(a%).s = a% / 10
MioRecord(a%).sS = Str$(a%)
MioRecord(a%).leng = Len(MioRecord(a%).sS)
Rem Put #numfile, , MioRecord(a%) <-- this is the old way to store data each record a time
Next a%
Put #numfile, , MioRecord()
Close #numfile
Print "Data file made"
GoSub WaitAKeyPressed
Return
WaitAKeyPressed:
Print "waiting a keypressed"
Do: s$ = InKey$: Loop Until s$ <> ""
Return
Failure:
Cls , 5
Color 30, 2
Print " System Failure! Data file not found!" + Chr$(13) + "Now Quitting"
End
Return
it follows the old fashion of GOSUB and GOTO but each GOSUB / RETURN can be switched into a SUB/END SUB with the right parameters.
While ON ERROR GOTO cannot be converted into modern Subroutine.
It shows the difference between Qbasic/QB45 and QB64pe: the news is that you can save and read a whole array to/from file in QB64pe.