Date: 02-16-2022
Return to Index
created by gbSnippets
'Compilable Example: (Jose Includes)
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
%IDC_Write = 400
%IDC_Read = 401
%IDC_Display = 402
Global hDlg As Dword, MyArray() As String
Global MyData() As Point 'any Type will do. Point has .x and .y members
Global ArraySize As Long, HeaderInfo As String * 9 'use any size HeaderInfo, but used fixed length string
Function PBMain() As Long
Dialog New Pixels, 0, "Array Read/Write Test Code",300,300,240,200, %WS_OverlappedWindow To hDlg
Control Add Button, hDlg, %IDC_Write, "Write / Erase", 30,40,130,25
Control Add Button, hDlg, %IDC_Read, "Read", 30,70,130,25
Control Add Button, hDlg, %IDC_Display, "Display", 30,100,130,25
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Local i As Long,temp$
Select Case Cb.Msg
Case %WM_InitDialog
'create test data
ArraySize = 3 'small value just for testing purposes
HeaderInfo = "My Header" 'I chose 9 character size. Use your own.
ReDim MyData(ArraySize) '0-4 elment indexes of type Point
For i = 0 To UBound(MyData)
MyData(i).x = Rnd(1,9) : MyData(i).y = Rnd(1,9) 'random data
Next i
Case %WM_Command
Select Case Cb.Ctl
Case %IDC_Write
Open "test.dat" For Binary As #1
Put #1,,HeaderInfo '9 bytes - the header - must be fixed string length
ArraySize = UBound(MyData)
Put #1,,ArraySize '4 bytes - the number of array elements
Put #1,,MyData() 'the entire array, regardless of how many array elements
Close #1
HeaderInfo = "" : ArraySize = 0 : Erase MyData() 'so when we read these, we know the data will be valid
Case %IDC_Read
Open "test.dat" For Binary As #1
Get #1,,HeaderInfo '9 bytes
Get #1,,ArraySize '4 bytes
ReDim MyData(ArraySize) 'resize array to number of array elements
Get #1,, MyData()
Close #1
Case %IDC_Display
temp$ = temp$ + "Header: " + HeaderInfo + $CrLf + "Array Size: " + Str$(ArraySize) + $CrLf
For i = 0 To UBound(MyData)
temp$ = temp$ + "MyData(" + Trim$(Str$(i)) + ").x" + Str$(MyData(i).x) + _
" MyData(" + Trim$(Str$(i)) + ").y" + Str$(MyData(i).y) + $CrLf
Next i
? temp$
End Select
End Select
End Function
'gbs_00850
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm