Results 1 to 10 of 10

Thread: [RESOLVED] How to read arrays from Binary Files?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2011
    Posts
    16

    Resolved [RESOLVED] How to read arrays from Binary Files?

    Hey everyone,
    How can I read arrays from Binary Files?

    Don't worry, I've got the answers now
    Last edited by usycool1; Oct 4th, 2011 at 11:30 AM.

  2. #2
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,388

    Re: How to read arrays from Binary Files?

    I would suggest storing the recipes in a a list(of FullRecipe) and then serializing the list(of FullRecipe) object... easy way to store and retrieve what you want ... follow the "File serialization with compression and encryption" link in my signature

    Kris

  3. #3
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: How to read arrays from Binary Files?

    Use a System.IO.BinaryWriter and System.IO.BinaryReader, they're quite convenient. Just call Write() on the BinaryWriter for each field, for each recipe. With the ArrayLists (which should be List(Of t)s, I suggest you change that as soon as possible) you would just write out the length, then each item. When reading it back, you would do something like:

    Code:
    Dim r As FullRecipe
    r.Name = reader.ReadString()
    r.Units = New List(Of String)
    For i As Integer = 1 To reader.ReadInt32()
        r.Units.Add(reader.ReadString())
    Next
    r.Ingredients = New List(Of String)
    For i As Integer = 1 To reader.ReadInt32()
        r.Ingredients.Add(reader.ReadString())
    Next
    r.Quantity = New List(Of String)
    For i As Integer = 1 To reader.ReadInt32()
        r.Quantity.Add(reader.ReadString())
    Next
    r.NumberOfPeople = reader.ReadInt32()
    
    'Done reading!
    You already have the right idea with LengthOfQuantityList, but it's not necessary - you can easily do Quantity.Length or Quantity.Count.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Oct 2011
    Posts
    16

    Re: How to read arrays from Binary Files?

    Thanks a lot, guys I will give them both a go

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Oct 2011
    Posts
    16

    Re: How to read arrays from Binary Files?

    Quote Originally Posted by minitech View Post
    Use a System.IO.BinaryWriter and System.IO.BinaryReader, they're quite convenient. Just call Write() on the BinaryWriter for each field, for each recipe. With the ArrayLists (which should be List(Of t)s, I suggest you change that as soon as possible) you would just write out the length, then each item. When reading it back, you would do something like:

    Code:
    Dim r As FullRecipe
    r.Name = reader.ReadString()
    r.Units = New List(Of String)
    For i As Integer = 1 To reader.ReadInt32()
        r.Units.Add(reader.ReadString())
    Next
    r.Ingredients = New List(Of String)
    For i As Integer = 1 To reader.ReadInt32()
        r.Ingredients.Add(reader.ReadString())
    Next
    r.Quantity = New List(Of String)
    For i As Integer = 1 To reader.ReadInt32()
        r.Quantity.Add(reader.ReadString())
    Next
    r.NumberOfPeople = reader.ReadInt32()
    
    'Done reading!
    You already have the right idea with LengthOfQuantityList, but it's not necessary - you can easily do Quantity.Length or Quantity.Count.
    Hi, whenever I try to use your example, this happens (Please see attachment).

    Do you know what I'm doing wrong?
    Last edited by usycool1; Oct 4th, 2011 at 11:28 AM.

  6. #6
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: How to read arrays from Binary Files?

    Can you post your writing code? You might not have mirrored the reading scheme quite right. Sorry for not posting that part.

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Oct 2011
    Posts
    16

    Re: How to read arrays from Binary Files?

    Quote Originally Posted by minitech View Post
    Can you post your writing code? You might not have mirrored the reading scheme quite right. Sorry for not posting that part.
    Yeah, sure here it is:
    Code:
    'Make a procedure which stores the values into a Binary File.
        Sub WriteToFile(ByVal WriteRecipe As Recipe, ByVal FileWriter As BinaryWriter)
            'Write everything to the Binary File.
            FileWriter.Write(WriteRecipe.Name)
            FileWriter.Write(WriteRecipe.NumberToServe)
    
            For Each x In WriteRecipe.Ingredients
                FileWriter.Write(x)
            Next
    
            For Each y In WriteRecipe.Units
                FileWriter.Write(y)
            Next
    
            For Each z In WriteRecipe.Quantities
                FileWriter.Write(z)
            Next
        End Sub
    Thanks a lot for your help, by the way, I really appreciate it

  8. #8
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: How to read arrays from Binary Files?

    No problem

    The problem is you didn't write the number of items before writing the items. Easy fix, though:

    Code:
        'Make a procedure which stores the values into a Binary File.
        Sub WriteToFile(ByVal WriteRecipe As Recipe, ByVal FileWriter As BinaryWriter)
            'Write everything to the Binary File.
            FileWriter.Write(WriteRecipe.Name)
            FileWriter.Write(WriteRecipe.NumberToServe)
    
            FileWriter.Write(WriteRecipe.Ingredients.Count)
            For Each x In WriteRecipe.Ingredients
                FileWriter.Write(x)
            Next
    
            FileWriter.Write(WriteRecipe.Units.Count)
            For Each y In WriteRecipe.Units
                FileWriter.Write(y)
            Next
    
            FileWriter.Write(WriteRecipe.Quantities.Count)
            For Each z In WriteRecipe.Quantities
                FileWriter.Write(z)
            Next
        End Sub

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Oct 2011
    Posts
    16

    Re: How to read arrays from Binary Files?

    Quote Originally Posted by minitech View Post
    No problem

    The problem is you didn't write the number of items before writing the items. Easy fix, though:

    Code:
        'Make a procedure which stores the values into a Binary File.
        Sub WriteToFile(ByVal WriteRecipe As Recipe, ByVal FileWriter As BinaryWriter)
            'Write everything to the Binary File.
            FileWriter.Write(WriteRecipe.Name)
            FileWriter.Write(WriteRecipe.NumberToServe)
    
            FileWriter.Write(WriteRecipe.Ingredients.Count)
            For Each x In WriteRecipe.Ingredients
                FileWriter.Write(x)
            Next
    
            FileWriter.Write(WriteRecipe.Units.Count)
            For Each y In WriteRecipe.Units
                FileWriter.Write(y)
            Next
    
            FileWriter.Write(WriteRecipe.Quantities.Count)
            For Each z In WriteRecipe.Quantities
                FileWriter.Write(z)
            Next
        End Sub
    Thanks a lot! It's working now

  10. #10
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: How to read arrays from Binary Files?

    My pleasure If your thread's resolved, you can mark it like that using the link in my signature or the Thread Tools menu at the top.

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