Date: 02-16-2022
Return to Index
created by gbSnippets
'With the PowerBASIC Get/Put statements, a single line of code can be
'used to read or write arrays to files opened in Binary mode.
'When writing to a Binary file, be sure to either truncate the file at the last
'byte, or Kill the file before Opening it.
'Primary Code:
Dim MyArray(5)
Array Assign MyArray() = 0,1,2,3,4,5
'PUT Array Data into file
Kill "myfile.txt" 'KILLing avoids worrying about size of BINARY file
Open "myfile.txt" For Binary As #1
Put #1, 1, MyArray() 'this one line writes the entire array
Close #1
'GET Array Data from file
Open "myfile.txt" For Binary As #1
Get #1, MyArray() 'GET reads enough values to fill the array
Close #1
'Compilable Example: (Jose Includes)
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
Global hDlg As Dword, MyArray() As Long
Function PBMain() As Long
Dim MyArray(5)
Array Assign MyArray() = 0,1,2,3,4,5
Dialog New Pixels, 0, "Test Code",300,300,200,200, %WS_OverlappedWindow To hDlg
Control Add Button, hDlg, 100,"PUT", 50,10,100,20
Control Add Button, hDlg, 200,"GET", 50,40,100,20
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Dim temp$, i as Long
If CB.Msg = %WM_Command AND CB.Ctl = 100 AND CB.Ctlmsg = %BN_Clicked Then
Kill "myfile.txt" 'KILLing avoids worrying about size of BINARY file
Open "myfile.txt" For Binary As #1
Put #1, 1, MyArray() 'PUT writes the entire array
Close #1
For i = 0 to 5 : temp$ = temp$ + Str$(MyArray(i)) + $crlf : Next i
MsgBox temp$
End If
If CB.Msg = %WM_Command AND CB.Ctl = 200 AND CB.Ctlmsg = %BN_Clicked Then
Reset MyArray() 'clear before reading new values
Open "myfile.txt" For Binary As #1
Get #1, 1, MyArray() 'GET reads enough values to fill the array
Close #1
For i = 0 to 5 : temp$ = temp$ + Str$(MyArray(i)) + $crlf : Next i
MsgBox temp$
End If
End Function
'gbs_00163
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm