Author Topic: UDT Array to Data File by Random Access Test  (Read 3227 times)

0 Members and 1 Guest are viewing this topic.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
UDT Array to Data File by Random Access Test
« on: December 24, 2021, 02:17:34 am »
Again inspired by conversation on Discord, this is something I did in 90's with old QB but never got around to testing with QB64 until now:
Code: QB64: [Select]
  1. _Title "UDT to Random Access File Test" ' b+ 2021-12-24
  2. Type Image
  3.     As String * 255 Text, FileName
  4.     As Long SzX, SzY, PosX, PosY
  5.  
  6. Const SW = 1000, SH = 700
  7. ReDim As Image TheItem(1 To 100), TheRecord
  8.  
  9. 'testing the FakeText$ function because RA is very strict on fixed strings!
  10. TheItem(1).Text = FakeText$(255)
  11. TheItem(1).FileName = FakeText$(255)
  12. Print TheItem(1).Text
  13. Print Len(TheItem(1).Text)
  14. Print TheItem(1).FileName
  15. Print Len(TheItem(1).FileName)
  16. Print "zzz"
  17.  
  18. 'make fake data to file
  19. For i = 1 To 100
  20.     TheItem(i).Text = FakeText$(255)
  21.     TheItem(i).SzX = 100 + Rnd * 20
  22.     TheItem(i).SzY = 70 + Rnd * 14
  23.     TheItem(i).PosX = Rnd * (SW - TheItem(i).SzX)
  24.     TheItem(i).PosY = Rnd * (SH - TheItem(i).SzY)
  25.     TheItem(i).FileName = FakeText$(255)
  26.  
  27. Open "Data Dump.RA" For Random As #1 Len = Len(TheRecord)
  28. For i = 1 To 100
  29.     'odious and tedious is this
  30.     TheRecord.Text = TheItem(i).Text
  31.     TheRecord.SzX = TheItem(i).SzX
  32.     TheRecord.SzY = TheItem(i).SzY
  33.     TheRecord.PosX = TheItem(i).PosX
  34.     TheRecord.PosY = TheItem(i).PosY
  35.     TheRecord.FileName = TheItem(i).FileName
  36.     Put #1, , TheRecord
  37. Print "Data File Ready"
  38.  
  39. ' OK we got data filed! Now can we get it back
  40. Open "Data Dump.RA" For Random As #1 Len = Len(TheRecord)
  41. For i = 1 To 100
  42.     Cls
  43.     Get #1, i, TheRecord
  44.     Print "Record Number:"; i
  45.     Print "Text: "; TheRecord.Text
  46.     Print "SzX:"; TheRecord.SzX
  47.     Print "SzY:"; TheRecord.SzY
  48.     Print "PosX:"; TheRecord.PosX
  49.     Print "PosY:"; TheRecord.PosY
  50.     Print "FileName:"; TheRecord.FileName
  51.     Print " zzz..."
  52.     Sleep
  53.  
  54. Function FakeText$ (lengthh)
  55.     BlankString$ = Space$(lengthh)
  56.     fini = Int(Rnd * 255) + 1
  57.     For i = 1 To fini
  58.         Mid$(BlankString$, i, 1) = Chr$(Rnd * (96 - 32) + 32)
  59.     Next
  60.     FakeText$ = BlankString$
  61.  

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: UDT Array to Data File by Random Access Test
« Reply #1 on: December 24, 2021, 02:43:46 am »
Here is the FixMe$ Function for inserting variable length strings into fixed length ones:

Code: QB64: [Select]
  1. _Title "FixMe$ function test" 'b+ 2021-12-24
  2.  
  3. 'so you want to make variable length strings into a fixed length
  4.  
  5. test$ = FixMe$("123456789", 20)
  6. Print "Before:"
  7. Print "123456789"; "<"
  8. Print "123456789012345678901234567890"
  9. Print test$; "<"
  10. Print Len(test$)
  11.  
  12. 'test oversized varible length
  13. Print "Before:"
  14. Print "1234567890123456789012345"
  15. test$ = FixMe$("1234567890123456789012345", 20)
  16. Print "123456789012345678901234567890"
  17. Print test$; "<"
  18. Print Len(test$)
  19.  
  20. Function FixMe$ (Me$, lengthh)
  21.     BlankString$ = Space$(lengthh)
  22.     Mid$(BlankString$, 1, lengthh) = Me$
  23.     FixMe$ = BlankString$
  24.  
  25.