Results 1 to 4 of 4

Thread: Reading strings in Binary file

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2005
    Posts
    80

    Smile Reading strings in Binary file

    Hi, I have a file (.bin) that is compressed with the following information:

    Color Food Color Food Color Food ............... (until end of file)

    For each string, it'd be 10 bytes long. So I'd read the first 10 bytes, it'd be color1, next 10 bytes, it'd be food1, next 10 bytes, would be color2.... etc.

    How do I:

    Process and read the binary file, and for each item read, throw into a structure of array

    VB Code:
    1. Public Structure AboutFoods
    2.    Dim Color as String
    3.    Dim Food as String
    4. End Structure

    So I'd have an array for Color, an array for Food?

    Thanks ....

  2. #2
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    Re: Reading strings in Binary file

    You can read a file in blocks using the StreamReader class, i.e.
    VB Code:
    1. Public Sub ReadBinFile(ByVal filename As String)
    2.       Dim colors As New ArrayList()
    3.       Dim food As New ArrayList()
    4.  
    5.       ' Open the file for read as text
    6.       Dim reader As IO.StreamReader = IO.File.OpenText(filename)
    7.       ' Create a 10 character buffer
    8.       Dim chars(9) As Char
    9.       ' Read the next 10 characters from the file
    10.       Do While reader.ReadBlock(chars, 0, 10) > 0
    11.          ' the first 10 characters will be a color
    12.          ' so add the string to the colors array
    13.          colors.Add(String.Concat(chars).Trim())
    14.          ' Read the next 10 characters from the file
    15.          If reader.ReadBlock(chars, 0, 10) > 0 Then
    16.             ' This block of characters should be
    17.             ' a food, so add it to the food array
    18.             food.Add(String.Concat(chars).Trim())
    19.          End If
    20.          ' Keep going until the whole file is read
    21.       Loop
    22.       ' Close the reader
    23.       reader.Close()
    24.  
    25.       ' Do whatever with the lists here
    26.       Console.WriteLine("Colors:")
    27.       For Each color As String In colors
    28.          Console.WriteLine(color)
    29.       Next
    30.       Console.WriteLine("Food:")
    31.       For Each item As String In food
    32.          Console.WriteLine(item)
    33.       Next
    34.    End Sub
    Regards,

    - Aaron.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Sep 2005
    Posts
    80

    Re: Reading strings in Binary file

    Thanks Aaron....

    But I want to also attempt in using this method other than streamreader class. Here's what I've been playing with....

    VB Code:
    1. Private Const OutPut As Integer = 1
    2.  
    3. Public Structure AboutFoods
    4.    Dim Color as String
    5.    Dim Food as String
    6. End Structure
    7.  
    8. Private Sub Process(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ....
    9.  
    10. Dim strFoods As New String(" "c, 10)
    11.  
    12. FileOpen(1, foodfile.BIN, OpenMode.Binary, OpenAccess.Read, OpenShare.Shared)  'Open .BIN file
    13.  
    14.         Do While Not EOF(1)
    15.             FileGet(1, strFoods)  'Read thru each 10 bytes
    16.  
    17.             '**How do you load Color and Food into AboutFoods.Color and
    18.            'AboutFoods.Food, and be able to access each item in array...
    19.            'or should I read 20 bytes each time, and split each record
    20.            '10 bytes to get Color and Food?
    21.  
    22.         Loop
    23.  
    24. End Sub

    And I'd want to be able to get # of records read in binary file, and each item in structure array.....

    Thanks again...

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Sep 2005
    Posts
    80

    Re: Reading strings in Binary file

    Anyone? .......

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