|
-
May 1st, 2006, 04:51 PM
#1
Thread Starter
Lively Member
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:
Public Structure AboutFoods
Dim Color as String
Dim Food as String
End Structure
So I'd have an array for Color, an array for Food?
Thanks ....
-
May 1st, 2006, 08:58 PM
#2
Re: Reading strings in Binary file
You can read a file in blocks using the StreamReader class, i.e.
VB Code:
Public Sub ReadBinFile(ByVal filename As String)
Dim colors As New ArrayList()
Dim food As New ArrayList()
' Open the file for read as text
Dim reader As IO.StreamReader = IO.File.OpenText(filename)
' Create a 10 character buffer
Dim chars(9) As Char
' Read the next 10 characters from the file
Do While reader.ReadBlock(chars, 0, 10) > 0
' the first 10 characters will be a color
' so add the string to the colors array
colors.Add(String.Concat(chars).Trim())
' Read the next 10 characters from the file
If reader.ReadBlock(chars, 0, 10) > 0 Then
' This block of characters should be
' a food, so add it to the food array
food.Add(String.Concat(chars).Trim())
End If
' Keep going until the whole file is read
Loop
' Close the reader
reader.Close()
' Do whatever with the lists here
Console.WriteLine("Colors:")
For Each color As String In colors
Console.WriteLine(color)
Next
Console.WriteLine("Food:")
For Each item As String In food
Console.WriteLine(item)
Next
End Sub
Regards,
- Aaron.
-
May 2nd, 2006, 12:48 AM
#3
Thread Starter
Lively Member
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:
Private Const OutPut As Integer = 1
Public Structure AboutFoods
Dim Color as String
Dim Food as String
End Structure
Private Sub Process(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ....
Dim strFoods As New String(" "c, 10)
FileOpen(1, foodfile.BIN, OpenMode.Binary, OpenAccess.Read, OpenShare.Shared) 'Open .BIN file
Do While Not EOF(1)
FileGet(1, strFoods) 'Read thru each 10 bytes
'**How do you load Color and Food into AboutFoods.Color and
'AboutFoods.Food, and be able to access each item in array...
'or should I read 20 bytes each time, and split each record
'10 bytes to get Color and Food?
Loop
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...
-
May 2nd, 2006, 11:45 AM
#4
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|