How do you write an array to a file?
Printable View
How do you write an array to a file?
Here is the easiest way to do it.....
Code:
Private Sub Form_Load()
Dim sArray(1 To 10) As String
Dim iCt As Integer
'Fill Array with random characters
For iCt = 1 To 10
sArray(iCt) = Chr(Rnd * 255)
Next iCt
'Open File to be written to
Open "C:\Array.txt" For Output As #1
For iCt = 1 To 10
' Write current array element to file
Write #1, sArray(iCt)
Next iCt
'Close File
Close #1
End Sub
It might be a good idea to write the length of the array before it.
Here's my Code.
Thank's YoungBuck for the idea of how to do it.
Public Key As Variant
Public ZipedText As String
Private Sub mnuOpen_Click()
Dim lenKey As Integer
FileName = ShowFileDialog(Me.hWnd)
Open FileName For Input As 1
Input #1, lenKey
ReDim Key(lenKey)
Do
r = r + 1
DoEvents
Input #1, Key(r)
If Key(r) = "End Array" Then
Exit Do
Else
End If
Loop
Do While Not EOF(1)
Input #1, zipedtxt
ZipedText = ZipedText + zipedtxt
Loop
Text1.text = unZip(ZipedText, Key)
Close
End Sub
Private Sub mnuSave_Click()
Key = Zip(Text1.text, ZipedText)
FileName = ShowFileDialog(Me.hWnd)
FileName = FileName & ".txt"
Open FileName For Output As 1
Write #1, UBound(Key)
For i = 1 To UBound(Key)
r = r + 1
If Key(r) = "" Then
Write #1, "End Array"
Exit For
End If
Write #1, Key(r)
Next
Write #1, ZipedText
Close
End Sub
arrays with fixed length data types are best stored binary, because it's fast and easy:
Code:Dim IntegerArray(10) as integer
Dim DynamicLong() as long,length as byte'or use integer for room for larger arrays
Dim UDTarray(20) as UDTname
length=50
redim Dynamiclong(length-1)
'to write
open file for binary as 1
put #1,,IntegerArray
put #1,,length
put #1,,DynamicLong
put #1,,UDTarray
'to read
get #1,1,IntegerArray 'reset to start position
get #1,,length
redim Dynamiclong(length-1)
get #1,,Dynamiclong
get #1,,UDTarray
close 1