dcsimg
Results 1 to 5 of 5

Thread: I Want to Save Array In A File I Created

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2018
    Posts
    16

    I Want to Save Array In A File I Created

    Hello Everyone,

    I had created a custom file in my vb program and now I want to save an array of integer values in this file and later on I want to read that array values from that file. I am attaching the code I used to create that file. Pls help.

    Code:
     path = "C:\Lmh\Data\" & path1 & "\Spirotest\test1_" & newpatientFormDateTimepicker1 & ".spr"
    
            ' Create or overwrite the file.
            Dim fs As FileStream
            If Not File.Exists(path) Then
                fs = File.Create(path)
            End If
            fs.Close()

  2. #2
    Addicted Member
    Join Date
    May 2017
    Location
    Italy
    Posts
    128

    Re: I Want to Save Array In A File I Created


  3. #3
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Pointless Forest 38.517,-92.023
    Posts
    9,042

    Re: I Want to Save Array In A File I Created

    The trick is to convert the integers into a format that can be written. That could be a string representation or byte representation. Here is some code that converts an array of integers into a list of bytes and back to integers.

    BitConverter

    Code:
            Dim arrayOfInts() As Integer = {Integer.MaxValue, 1, 2, 3, Integer.MinValue}
            Dim fileBuffout As New List(Of Byte)
    
            For Each i As Integer In arrayOfInts
                fileBuffout.AddRange(BitConverter.GetBytes(i))
            Next
    
            Dim buf() As Byte = fileBuffout.ToArray
    
            'convert bytes back to integer
            Dim listOfInts As New List(Of Integer)
            For x As Integer = 0 To buf.Length - 1 Step 4
                Dim i As Integer = BitConverter.ToInt32(buf, x)
                listOfInts.Add(i)
            Next
            Stop 'examine all variables
    Run that in the debugger and watch what happens. Once you have done that, reading these two links should get you the desired result.

    https://msdn.microsoft.com/en-us/lib...v=vs.110).aspx

    https://msdn.microsoft.com/en-us/lib...v=vs.110).aspx
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jan 2018
    Posts
    16

    Re: I Want to Save Array In A File I Created

    Thank you dbasnett for your support.

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    32,372

    Re: I Want to Save Array In A File I Created

    You also have a thread about working with a database. If it is for the same program, you might as well use the same database. A single extra table could hold the data from the list. It could be a table with a single field of type Int.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width