Trouble writing/reading a binary file

Black

New member
Joined
Nov 6, 2012
Messages
2
Programming Experience
3-5
So i'm a total noob in visual basic.net, and I need to write some data to a binary file so it can be retrived in the next execution.

The variable that is writing is a class(Targets) containing an array of another class named t, the sub-class(target) contains the real data in 22 variables, also the class targets has an integer counter of how many instances are in the array(nfarms), hope it's clear, i can re-explain if needed.

The problem is that when the program tries to read the file gets stuck at the first line of the while, at Targets.t(x).x = objBR.ReadString() from the reading function, but the program do not crashes, just stops, maybe i made a mistake on the writing or the reading functions, i dont know.

The file generates correctly and the program is able to retrive the nfarms value correctly(the first one written), bun when it enters into the loop, fails.

Thanks in advance!

This is the writing function:

VB.NET:
    Public Sub save_target_file()
        Dim objBW As BinaryWriter
        Dim objFS As FileStream
        Dim x As Integer
        x = 1
        Testingbox.AppendText(Environment.NewLine + "Nfarms " + Targets.nfarms.ToString)
        If (Targets.nfarms > 0) Then

            objFS = New FileStream("Targets.bin", FileMode.OpenOrCreate, FileAccess.Write)
            objBW = New BinaryWriter(objFS)
            objBW.BaseStream.Seek(0, SeekOrigin.Begin)

            objBW.Write(Targets.nfarms)

            While (x <= Targets.nfarms)
                Testingbox.AppendText(Environment.NewLine + "Writing farm number " + x.ToString)

                objBW.Write(Targets.t(x).x)
                objBW.Write(Targets.t(x).y)
                objBW.Write(Targets.t(x).name)
                objBW.Write(Targets.t(x).race)

                objBW.Write(Targets.t(x).priority)
                objBW.Write(Targets.t(x).distance)
                objBW.Write(Targets.t(x).raid_ratio)
                objBW.Write(Targets.t(x).num_atacks)
                objBW.Write(Targets.t(x).total_profit)
                objBW.Write(Targets.t(x).auto_balance)
                objBW.Write(Targets.t(x).reset_raid_time)
                objBW.Write(Targets.t(x).never_raided)
                objBW.Write(Targets.t(x).min_atack_capacity)

                objBW.Write(Targets.t(x).min_raid_time_interval)
                objBW.Write(Targets.t(x).max_raid_time_interval)
                objBW.Write(Targets.t(x).time_remaining)

                objBW.Write(Targets.t(x).population)
                objBW.Write(Targets.t(x).wall)
                objBW.Write(Targets.t(x).palace)
                objBW.Write(Targets.t(x).traps)

                objBW.Write(Targets.t(x).Activated)
                objBW.Write(Targets.t(x).inactive)

                Testingbox.AppendText(Environment.NewLine + "finalized Writing farm number " + x.ToString)
                x = x + 1
            End While
            objBW.Close()
            objFS.Close()
        End If

    End Sub

And the reading function:

VB.NET:
 Public Sub load_target_file()
        Dim x As Integer
        Dim objBR As BinaryReader
        Dim objFS As FileStream
        x = 1

        If File.Exists("Targets.bin") Then
            'open the file
            Testingbox.AppendText(Environment.NewLine + "File exists")
            objFS = New FileStream("Targets.bin", FileMode.Open, FileAccess.Read)
            objBR = New BinaryReader(objFS)
            objBR.BaseStream.Seek(0, SeekOrigin.Begin)


            Targets.nfarms = objBR.ReadInt32() 'read the number of farms
            Testingbox.AppendText(Environment.NewLine + "Farms detected: " + Targets.nfarms.ToString)

            While (x <= Targets.nfarms)
                Testingbox.AppendText(Environment.NewLine + "Farm " + x.ToString + "is being read")

                Targets.t(x).x = objBR.ReadString()
                Targets.t(x).y = objBR.ReadString()
                Targets.t(x).name = objBR.ReadString()
                Targets.t(x).race = objBR.ReadString()

                Targets.t(x).priority = objBR.ReadInt32()
                Targets.t(x).distance = objBR.ReadInt32()
                Targets.t(x).raid_ratio = objBR.ReadInt32()
                Targets.t(x).num_atacks = objBR.ReadInt32()
                Targets.t(x).total_profit = objBR.ReadInt32()
                Targets.t(x).auto_balance = objBR.ReadBoolean()
                Targets.t(x).reset_raid_time = objBR.ReadBoolean()
                Targets.t(x).never_raided = objBR.ReadBoolean()
                Targets.t(x).min_atack_capacity = objBR.ReadInt32()

                Targets.t(x).min_raid_time_interval = objBR.ReadInt32()
                Targets.t(x).max_raid_time_interval = objBR.ReadInt32()
                Targets.t(x).time_remaining = objBR.ReadInt32()

                Targets.t(x).population = objBR.ReadInt32()
                Targets.t(x).wall = objBR.ReadInt32()
                Targets.t(x).palace = objBR.ReadInt32()
                Targets.t(x).traps = objBR.ReadBoolean()

                Targets.t(x).Activated = objBR.ReadBoolean()
                Targets.t(x).inactive = objBR.ReadBoolean()

                Testingbox.AppendText(Environment.NewLine + "Farm " + x.ToString + "Readed")
                x = x + 1
            End While
            objBR.Close()
            objFS.Close()
            Testingbox.AppendText(Environment.NewLine + "File closed")
        End If
    End Sub
 
when it enters into the loop, fails.
Do you mean NullReferenceException?

Why not serialize the data with BinaryFormatter?
 
Do you mean NullReferenceException?

Why not serialize the data with BinaryFormatter?


No, the program stops, and is unable to hadle events, but no crash, i've got a textbox to track where the program stops, and the last line printed is the one that corresponds with the first loop:

Testingbox.AppendText(Environment.NewLine + "Farm " + x.ToString + "is being read")

This is my first expirience with vb.net, and i have no idea on how the whole file managment work, what do you mean with serialize the data with BinaryFormatter??

Thanks!
 
Are you sure the data types matches? For example that x and y properties are type String where you attempt to use ReadString and so on.

Here's some examples I found when I searched web for "vb.net binaryformatter": How to Serialize Data in VB.NET - Code Notes
 
Also it's good practice to get into the habit of trapping exceptions using Try...Catch blocks and using the debug tools available to you.

There really shouldn't be any need to textboxes to track values and states - drop a breakpoint into your code and you'll see exactly what's going on at that point.
 

Latest posts

Back
Top