none
BinaryRead & BinaryWrite RRS feed

  • Question

  • Windows 8.1 : Visual Studio 2017 : VB

    I'm trying to write an App to use on my Desktop PC.   The App needs to load two Integer arrays at startup. I then need to amend (update) them and save them back to file again on shutdown.

     I'm a self-taught programmer and not too far advanced.  I have been trying to use binaryWrite/BinaryRead by following examples on the internet searches.  Even tried the examples on Microsoft's pages, but I can't get them to work.  I can read & write text files, but reading and saving integer arrays is beyond me.

     I'm 77 years of age and ( I was going to say, "I'm dying to get it solved", but I don't want to push my luck too far.) but I would really appreciate some help.

    Ray


    XIXray

    Monday, August 14, 2017 8:45 PM

All replies

  • Windows 8.1 : Visual Studio 2017 : VB

    I'm trying to write an App to use on my Desktop PC.   The App needs to load two Integer arrays at startup. I then need to amend (update) them and save them back to file again on shutdown.

     I'm a self-taught programmer and not too far advanced.  I have been trying to use binaryWrite/BinaryRead by following examples on the internet searches.  Even tried the examples on Microsoft's pages, but I can't get them to work.  I can read & write text files, but reading and saving integer arrays is beyond me.

     I'm 77 years of age and ( I was going to say, "I'm dying to get it solved", but I don't want to push my luck too far.) but I would really appreciate some help.

    Ray


    XIXray

    Dear spring chicken,

    ;-)

    It sounds like you're asking about data persistence, am I right? How to "save and restore the state".

    If so, on the high end of it, binary serialization is fast and makes for a small file but you might also consider saving to/restoring from XML or, for that matter, just a .csv file.

    Does that sound like what you're wanting to do?

    ***** EDIT *****

    Also, you can use Application Settings for this:

    https://docs.microsoft.com/en-us/dotnet/visual-basic/developing-apps/programming/app-settings/how-to-persist-user-settings

    I'd rather write my own but that's not to say that they won't work. :)


    "A problem well stated is a problem half solved.” - Charles F. Kettering


    • Edited by Frank L. Smith Monday, August 14, 2017 9:19 PM ... MSDN Link
    Monday, August 14, 2017 9:09 PM
  • Hi

    There may be other ways to achieve your objective, in particular there are better collection types that you could use. However, if you really do want to do it with binaryWrite/BinaryRead, then here is an example (one of many possibilities)

    Since this is only an example: the first run will create 2 test data files and put them on your desktop. Once you have run once, then quit the application and comment out the lines in the Load event handler. To see the result of re-loading the data, put a breakpoint in the Button2 clcik event handler and examine the incoming data. (you can do same in the Button1 handler if you want to examine the outgoing data)

    ' Form1 with 2 buttons
    ' Button1 for Save arrys
    ' Button2 for Read arrays
    Option Strict On
    Option Explicit On
    Public Class Form1
        Dim a(100) As Integer
        Dim b(100) As Integer
        Dim pathA As String = My.Computer.FileSystem.SpecialDirectories.Desktop & "\FileA.xml"
        Dim pathB As String = My.Computer.FileSystem.SpecialDirectories.Desktop & "\FileB.xml"
        Private Sub Form1_Load(sender As System.Object, e As EventArgs) Handles MyBase.Load
    
            ' for testing - to build test arrays for first run
            For i As Integer = 0 To 99
                a(i) = i
                b(i) = i * 100
            Next
    
        End Sub
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            SaveArray(pathA, a)
            SaveArray(pathB, b)
        End Sub
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            a = CType(ReadArray(pathA), Integer())
            b = CType(ReadArray(pathB), Integer())
        End Sub
        Public Sub SaveArray(path As String, arr As Array)
            Dim fs As IO.FileStream = New IO.FileStream(path, IO.FileMode.OpenOrCreate)
            Dim bf As New Runtime.Serialization.Formatters.Binary.BinaryFormatter()
            bf.Serialize(fs, arr)
            fs.Close()
        End Sub
        Public Function ReadArray(path As String) As Array
            Dim arr() As Integer = Nothing
            If FileIO.FileSystem.FileExists(path) Then
                Dim fsRead As New IO.FileStream(path, IO.FileMode.Open)
                Dim bf As New Runtime.Serialization.Formatters.Binary.BinaryFormatter()
                arr = CType(bf.Deserialize(fsRead), Integer())
                fsRead.Close()
            End If
            Return arr
        End Function
    End Class


    Regards Les, Livingston, Scotland

    Monday, August 14, 2017 9:18 PM
  • Hi Ray,

    If your issue is how to save array into file and store data into array by reading the file, you can use a CSV file.
    If you provide two integer arrays, I will try (array date > CSV file, and CSV file > array).

    Remember that you posted the similar question and I provided a sample project.
    Have you tried my sample?
     I wish to save a fifty element integer array to file I then want to reload it to my app everytime I restart the App. I'm a NewBee even though I'm 77
     https://social.msdn.microsoft.com/Forums/vstudio/en-US/667476cd-833e-4a24-9c55-3b53ce0e7824/i-wish-to-save-a-fifty-element-integer-array-to-file-i-then-want-to-reload-it-to-my-app-everytime-i?forum=netfxbcl#667476cd-833e-4a24-9c55-3b53ce0e7824

    Best regards,

    Ashidacchi



    • Edited by Ashidacchi Tuesday, August 15, 2017 2:12 AM
    Tuesday, August 15, 2017 2:08 AM