|
-
Jan 7th, 2004, 05:07 PM
#1
Thread Starter
pathfinder
[Rslvd With "Option Strict Off"] Integer Array, FilePut, FileGet
I can't seem to find a way of writeing an integer array to a file, and reading it back.
I thought I almost had it:
VB Code:
Dim MyTArr() As Integer
Dim MyBArr() As Integer
Dim MyF As Integer
ReDim MyTArr(10)
ReDim MyBArr(10)
For MyF = 99 To 109
MyTArr(MyF - 99) = MyF
Next
'Place Integer Array into file
MyF = FreeFile()
FileOpen(MyF, "C:\test.tst", OpenMode.Binary)
FilePut(MyF, MyTArr)
FileClose(MyF)
'Read Data in file into another integer array
MyF = FreeFile()
FileOpen(MyF, "C:\test.tst", OpenMode.Binary)
FileGet(MyF, MyBArr)
FileClose(MyF)
'AGGGHHH!!! Theres that Darned Blue Snake Of Death!!!!
But the dreaded Blue Snake of Death appears under my FileGet!
How Can an Integer Array be put into a file, then later retrieved?
Last edited by NotLKH; Jan 7th, 2004 at 06:10 PM.
-
Jan 7th, 2004, 05:48 PM
#2
Frenzied Member
Your code works fine here, though I would rather use IO.Stream classes to do such task.
'Heading for the automatic overload'
Marillion, Brave, The Great Escape, 1994
'How will WE stand the FIRE TOMORROW?'
Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979
-
Jan 7th, 2004, 05:49 PM
#3
Thread Starter
pathfinder
Hmm.
I Had "Option Strict On" Active.
Turning it off fixed my problem.
-
Jan 7th, 2004, 05:52 PM
#4
PowerPoster
Why are you not using the System.IO namespace?
Anyway, another quick way (although probably not the 'right' way) would be to put those values in a dataset. Then just use the ds.WriteXML and ds.ReadXML methods. Only takes one line of code to write it out to disk, and it is in xml, which means you could do some other cool stuff with it if you really wanted or needed to.
Just a suggestion.
-
Jan 7th, 2004, 06:00 PM
#5
Thread Starter
pathfinder
Lunatic & HellsWraith: Good Thought! But I had already tried StreamWriter & StreamReader, and it seems to me that it will not print Integer Arrays??? Unless you can provide an Example???
Also, BinaryWriter was insisting on Bytes, and I have no Idea on how to convert an Int Array into a Byte Array in VBnet.
XML?
Haven't heard of ds.writexml yet. Sounds Interesting.
How well can it handle 200,000 sets of 37 integers in 12 hours?
Although I will probably shorten the 37 down to 18, plus a defintion of the formulas to resolve the other 19.
-
Jan 7th, 2004, 06:47 PM
#6
You could also use serialization.
VB Code:
Dim MyTArr() As Integer
Dim MyBArr() As Integer
Dim MyF As Integer
ReDim MyTArr(10)
ReDim MyBArr(10)
For MyF = 99 To 109
MyTArr(MyF - 99) = MyF
Next
'Place Integer Array into file
Dim bf As New Runtime.Serialization.Formatters.Binary.BinaryFormatter
Dim fs As New IO.FileStream("C:\test.tst", IO.FileMode.Create)
bf.Serialize(fs, MyTArr)
fs.Close()
fs = New IO.FileStream("C:\test.tst", IO.FileMode.Open)
MyBArr = bf.Deserialize(fs)
fs.Close()
MsgBox(MyBArr.Length)
As for speed, using serialization 200,000 arrays of 37 integers each can be written in just under 16 sec (on my computer) and read from the file in just under 50 sec. Also because of the way the JIT works those numbers should go down dramatically if the routine is repeated within the same app session. Here is what I used to test:
VB Code:
<Serializable()> _
Public Structure ArrayHolder
Public Inner() As Integer
End Structure
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim masterArray() As ArrayHolder
Dim resultArray() As ArrayHolder
Dim main(200000) As ArrayHolder
For cnt As Integer = 1 To 200000
Dim inner(36) As Integer
For inner_cnt As Integer = 1 To 37
inner(inner_cnt - 1) = inner_cnt
Next
main(cnt - 1).Inner = inner
Next
masterArray = main
Dim starttime As DateTime = DateTime.Now
'Place Integer Array into file
Dim sbf As New Runtime.Serialization.Formatters.Binary.BinaryFormatter
Dim ifs As New IO.FileStream("C:\test.txt", IO.FileMode.Create)
sbf.Serialize(ifs, masterArray)
ifs.Close()
MsgBox(String.Format("Serialization Time: {0}", DateTime.Now.Subtract(starttime).ToString))
starttime = DateTime.Now
ifs = New IO.FileStream("C:\test.txt", IO.FileMode.Open)
resultArray = sbf.Deserialize(ifs)
ifs.Close()
MsgBox(String.Format("Deserialization Time: {0}", DateTime.Now.Subtract(starttime).ToString))
MsgBox(resultArray.Length)
End Sub
The resulting file is almost 33MB.
-
Jan 8th, 2004, 07:00 AM
#7
Thread Starter
pathfinder
This "Serialization" technique looks really good!
I think I'll be useing this for many things.
Hmmm, "ArrayHolder".
Very Interesting!
Thanks,
-Lou
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
|