Results 1 to 7 of 7

Thread: [Rslvd With "Option Strict Off"] Integer Array, FilePut, FileGet

  1. #1

    Thread Starter
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397

    [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:
    1. Dim MyTArr() As Integer
    2.         Dim MyBArr() As Integer
    3.         Dim MyF As Integer
    4.         ReDim MyTArr(10)
    5.         ReDim MyBArr(10)
    6.         For MyF = 99 To 109
    7.             MyTArr(MyF - 99) = MyF
    8.         Next
    9.         'Place Integer Array into file
    10.         MyF = FreeFile()
    11.         FileOpen(MyF, "C:\test.tst", OpenMode.Binary)
    12.         FilePut(MyF, MyTArr)
    13.         FileClose(MyF)
    14.         'Read Data in file into another integer array
    15.         MyF = FreeFile()
    16.         FileOpen(MyF, "C:\test.tst", OpenMode.Binary)
    17.         FileGet(MyF, MyBArr)
    18.         FileClose(MyF)
    19.         '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?

  2. #2
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    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

  3. #3

  4. #4
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    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.

  5. #5

    Thread Starter
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397
    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.


  6. #6
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    You could also use serialization.
    VB Code:
    1. Dim MyTArr() As Integer
    2.         Dim MyBArr() As Integer
    3.         Dim MyF As Integer
    4.         ReDim MyTArr(10)
    5.         ReDim MyBArr(10)
    6.         For MyF = 99 To 109
    7.             MyTArr(MyF - 99) = MyF
    8.         Next
    9.  
    10.         'Place Integer Array into file
    11.         Dim bf As New Runtime.Serialization.Formatters.Binary.BinaryFormatter
    12.         Dim fs As New IO.FileStream("C:\test.tst", IO.FileMode.Create)
    13.         bf.Serialize(fs, MyTArr)
    14.         fs.Close()
    15.  
    16.         fs = New IO.FileStream("C:\test.tst", IO.FileMode.Open)
    17.         MyBArr = bf.Deserialize(fs)
    18.         fs.Close()
    19.  
    20.         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:
    1. <Serializable()> _
    2.     Public Structure ArrayHolder
    3.         Public Inner() As Integer
    4.     End Structure
    5.  
    6.     Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    7.         Dim masterArray() As ArrayHolder
    8.         Dim resultArray() As ArrayHolder
    9.         Dim main(200000) As ArrayHolder
    10.         For cnt As Integer = 1 To 200000
    11.             Dim inner(36) As Integer
    12.             For inner_cnt As Integer = 1 To 37
    13.                 inner(inner_cnt - 1) = inner_cnt
    14.             Next
    15.             main(cnt - 1).Inner = inner
    16.         Next
    17.         masterArray = main
    18.  
    19.         Dim starttime As DateTime = DateTime.Now
    20.         'Place Integer Array into file
    21.         Dim sbf As New Runtime.Serialization.Formatters.Binary.BinaryFormatter
    22.         Dim ifs As New IO.FileStream("C:\test.txt", IO.FileMode.Create)
    23.         sbf.Serialize(ifs, masterArray)
    24.         ifs.Close()
    25.         MsgBox(String.Format("Serialization Time: {0}", DateTime.Now.Subtract(starttime).ToString))
    26.  
    27.         starttime = DateTime.Now
    28.         ifs = New IO.FileStream("C:\test.txt", IO.FileMode.Open)
    29.         resultArray = sbf.Deserialize(ifs)
    30.         ifs.Close()
    31.         MsgBox(String.Format("Deserialization Time: {0}", DateTime.Now.Subtract(starttime).ToString))
    32.  
    33.         MsgBox(resultArray.Length)
    34. End Sub

    The resulting file is almost 33MB.

  7. #7

    Thread Starter
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397
    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
  •  



Click Here to Expand Forum to Full Width