|
-
Aug 31st, 2000, 12:16 PM
#1
Thread Starter
New Member
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
-
Aug 31st, 2000, 12:18 PM
#2
Frenzied Member
Text1.text = Array(1) is that what you needed
-
Aug 31st, 2000, 12:24 PM
#3
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
-
Aug 31st, 2000, 12:31 PM
#4
transcendental analytic
If you open your file in binary, your file will be much smaller, useful especially if you have large arrays.
Code:
dim ff as integer
ff=freefile
Open file for binary as ff
put#ff,,Arrayname
close ff
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.
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Aug 31st, 2000, 12:40 PM
#5
_______
<?>
...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
"A myth is not the succession of individual images,
but an integerated meaningful entity,
reflecting a distinct aspect of the real world."
___ Adolf Jensen
-
Aug 31st, 2000, 01:21 PM
#6
Fanatic Member
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...
-
Sep 1st, 2000, 01:09 AM
#7
transcendental analytic
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
Code:
'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
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 long
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|