Does anyone understand how to read an array and then place into a text file for later use?
I need to create an array that will loop through a series of numbers and then place those numbers into a text file for later use.
Please help,
Thanks
Rey
Printable View
Does anyone understand how to read an array and then place into a text file for later use?
I need to create an array that will loop through a series of numbers and then place those numbers into a text file for later use.
Please help,
Thanks
Rey
Text1.text = Array(1) is that what you needed :confused:
This will save an Array.
Code:'Create the Array
Dim MyArray(5) As Byte
MyArray(0) = "23"
MyArray(1) = "45"
MyArray(2) = "22"
MyArray(3) = "1"
MyArray(4) = "4"
MyArray(5) = "43"
'Save the Array
Open "MyFile.txt" For Output As #1
For I = LBound(MyArray) To UBound(MyArray)
Print #1, MyArray(I)
Next I
Close #1
If you open your file in binary, your file will be much smaller, useful especially if you have large arrays.
If you use dynamic arrays, you should put the dimensions before the array so that you can redimension it when you need to load it back from the file.Code:dim ff as integer
ff=freefile
Open file for binary as ff
put#ff,,Arrayname
close ff
...my 2 cents
Code:Option Explicit
Private Sub Command2_Click()
'read them into a file
Dim myArr(1 To 20)
Dim i As Integer
Dim intNum As Integer
intNum = FreeFile
Dim yourfile As String
yourfile = "C:\my documents\myfile.txt"
Open yourfile For Output As intNum
Randomize
For i = 1 To 20
myArr(i) = i & CInt(Rnd * 40)
Print #intNum, myArr(i)
Next i
Close intNum
End Sub
'read them from a file
Private Sub Command1_Click()
Dim myArr()
Dim i As Integer
'let's say you are stored in a file called your myfile
Dim intNum As Integer
intNum = FreeFile
Dim yourfile As String
yourfile = "C:\my documents\myfile.txt"
Open yourfile For Input As intNum
Do Until EOF(intNum)
i = i + 1
ReDim Preserve myArr(i)
Input #intNum, myArr(i)
Loop
Close intNum
End Sub
HeSaidJoe, I say hi.
Don't you think the ReDim statement is a little bit misplaced here in the Do...Loop iteration?
I would ReDim (without Preserve) the table before the loop with a certain maximum, and after the file has been read a ReDim Preserve with the number of records read.
That's how I would do it, I don't know if there's a difference...
The difference is that you could put anything into the file, but to redim it all the time would be slow, especially for large files, i recommend again you open in binary and get the array with get statement, write it with put.
As i said To write the upperbound would be a solution
note that you would have to use integer, if you have arrays larger than 255, if smaller you could use byte, but to be sure with huge arrays use longCode:'To write
Dim UB as integer
UB=Ubound(Arrayname)
Put #1,,UB
Put #1,,Arrayname
'To read
Get #1,,UB
Redim Arrayname(UB)
Get #1,Arrayname