Date: 02-16-2022
Return to Index
created by gbSnippets
'in general, if the length of array elements is fixed, using GET/PUT is
'the easiest, and fastest approach to putting array data into an array
'if the array elements are NOT of fixed length (such as dynamic strings) then
'writing each array element, followed by a delimiter (such as $crlf) is commonly used.
'it's also possible to precede each dynamic string with a Long value (any number
'type will do) which specifies the length of each string that follows. this is
'useful when a delimeter cannot be defined which is guaranteed NOT to be
'found in the strings
'Primary Code:
'Example#1 - use Print# to write dynamic string elements to a file, separated by $crlf
Open "test.txt" For Output As #1
Dim i As Long, MyArray(5) as Long
Array Assign MyArray() = 1,2,3,4,5,6 'sample data
For i = 0 To UBound(MyArray)
Print #1, MyArray(i) 'automatically places a $crlf in front of MyArray(i)
Next i
Close #1
'read back the values:
Open "test.txt" For Output As #1
Dim i As Long, MyArray(5) as Long
For i = 0 To UBound(MyArray)
Line Input #1, MyArray(i)
Next i
Close #1
'Example#2 - use Put# to write entire dynamic array in one step
'this DOES add $crlf between array element to put each value on a line of its own
Dim MyArray(5) as String, temp$
Array Assign MyArray() = "0","1","2","3","4","5" 'sample data
temp$ = Join$(MyArray(), $crlf)
Open "test.txt" For Binary as #1
Put #1,, temp$
Close #1
'read back the array values:
Dim MyArray() as Long
Open "test.txt" For Binary as #1
Get #1,, temp$
Close #1
Parse temp$, MyArray(), $crlf
'Example#3 - use Put# to write entire numeric array in one step
'this DOES NOT add $crlf between array element
Dim MyArray(5) as Long
Array Assign MyArray() = 1,2,3,4,5,6 'sample data
Open "test.txt" For Binary as #1
Put #1,, MyArray
Close #1
'read back the array values:
Dim MyArray() as Long
Open "test.txt" For Binary as #1
Get #1,, MyArray
Close #1
'Compilable Example: (Jose Includes)
'each of the examples place array values into files, then read back the values
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
Global hDlg As Dword, sArray() As String, iArray() As Long
Function PBMain() As Long
Dialog New Pixels, 0, "Array Read/Write Test Code",300,300,240,200, %WS_OverlappedWindow To hDlg
Control Add Label, hDlg, 100, "Array reset each time to: 0 1 2 3 4 5 6 7 8", 30,10,200,25
Control Add Button, hDlg, 200, "Dynamic Array - Print/Line Input Statements", 10,40,220,25
Control Add Button, hDlg, 300, "Dynamic Array - Get/Put Statements", 10,70,220,25
Control Add Button, hDlg, 400, "Numeric Array - Get/Put Statements", 10,100,220,25
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Local temp$, i As Long
If Cb.Msg = %WM_Command And Cb.CtlMsg = %BN_Clicked Then
Select Case Cb.Ctl
Case 200
ReDim sArray(8)
For i = 0 To 8 : sArray(i) = Str$(i) : Next i
Open "test.txt" For Output As #1
For i = 0 To UBound(sArray) : Print #1, sArray(i) : Next i 'write
Close #1
Open "test.txt" For Input As #1
For i = 0 To UBound(sArray) : Line Input #1, sArray(i) : Next i 'read
Close #1
temp$ = Join$(sArray(), $CrLf)
Case 300
ReDim sArray(8)
For i = 0 To 8 : sArray(i) = Str$(i) : Next i
temp$ = Join$(sArray(), $CrLf)
Open "test.txt" For Binary As #1 : Put #1,, temp$ : Close #1 'write
Open "test.txt" For Binary As #1 : Get #1,, temp$ : Close #1 'read
Parse temp$, sArray(), $CrLf
temp$ = Join$(sArray(), $CrLf)
Case 400
ReDim iArray(8) As Long
For i = 0 To 8 : iArray(i) = i : Next i
Open "test.txt" For Binary As #1 : Put #1,, iArray() : Close #1 'write
Open "test.txt" For Binary As #1 : Get #1,, iArray() : Close #1 'read
For i = 0 To 8 : temp$ = temp$ + $CrLf + Str$(iArray(i)) : Next i
End Select
MsgBox temp$
End If
End Function
'gbs_00070
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm