Reading Binary File containing Strings and Numeric Data

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 9053 Views - Last Post: 04 September 2012 - 11:59 PM Rate Topic: -----

#1 Jogiya  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 04-September 09

Reading Binary File containing Strings and Numeric Data

Posted 05 September 2009 - 09:45 AM

I'm having problem reading Binary file that has two types of data in it; Strings and numeric data in decimal format.


Dim ReadBinFile As FileStream
		Dim Strings As String
		Dim Numbers As Decimal
		ReadBinFile = New IO.FileStream("C:\temp\ass1\binfile.dat", IO.FileMode.Open)

		Dim ReadBinary As New BinaryReader(ReadBinFile)

		Do While ReadBinary.PeekChar <> -1
			Strings = ReadBinary.ReadString()
			Numbers = ReadBinary.ReadDecimal()

			MsgBox(Strings)
			MsgBox(Numbers)
			Dim rvBW As New IO.BinaryWriter(New IO.FileStream("C:\temp\ass1\readbinfile.txt", IO.FileMode.Create))
			rvBW.Write(Strings)
			rvBW.Write(Numbers)
			rvBW.Close()
		Loop
		ReadBinFile.Close()


The code just doesn't work properly..

1. The loop doesn't stop, and it gives me "Unable to read beyond the end of the stream" Error..

2. The output isn't accurate, apart from a few random correct strings and numbers..

3. After the loop is fixed, how can i save the data to an Array or write it to a text file?. Can the be saved line by line so that the output file has the same number of lines?

4. As you can see in the code, I've tried writing data in a file, but it doesn't work.. Messagebox does show some data (even though it's not accurate), the file however doesn't contain any data..

Please help...

Is This A Good Question/Topic? 0
  • +

Replies To: Reading Binary File containing Strings and Numeric Data

#2 piman314  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 32
  • View blog
  • Posts: 169
  • Joined: 07-August 09

Re: Reading Binary File containing Strings and Numeric Data

Posted 05 September 2009 - 09:51 AM

I would suggest using a StreamReader for reading a file like this. That way you can read in all the data, or line by line, and then use String.Split to split the data based on delimiters. The decimals can be retrieved from the strings by using Decimal.Parse. Could you post some of the file or at least the format of the file?
Was This Post Helpful? 0
  • +
  • -

#3 Jogiya  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 04-September 09

Re: Reading Binary File containing Strings and Numeric Data

Posted 05 September 2009 - 10:12 AM

View Postpiman314, on 5 Sep, 2009 - 08:51 AM, said:

I would suggest using a StreamReader for reading a file like this. That way you can read in all the data, or line by line, and then use String.Split to split the data based on delimiters. The decimals can be retrieved from the strings by using Decimal.Parse. Could you post some of the file or at least the format of the file?


I tried using StreamReader but that only worked with the strings..

		Dim vBinFile As String = "C:\temp\ass1\binfile.dat"
		Dim rvSRTF As New IO.StreamReader(vBinFile)
		Do While rvSRTF.Peek <> -1
			vText = rvSRTF.ReadLine()
			'Debug.WriteLine(vText)
			MsgBox(vText)
		Loop



I've attached the bin file..

Attached File(s)


This post has been edited by Jogiya: 05 September 2009 - 10:16 AM

Was This Post Helpful? 0
  • +
  • -

#4 piman314  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 32
  • View blog
  • Posts: 169
  • Joined: 07-August 09

Re: Reading Binary File containing Strings and Numeric Data

Posted 05 September 2009 - 10:20 AM

*EDIT
it didn't show the file the first time i read it... looking at it now

This post has been edited by piman314: 05 September 2009 - 10:21 AM

Was This Post Helpful? 0
  • +
  • -

#5 piman314  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 32
  • View blog
  • Posts: 169
  • Joined: 07-August 09

Re: Reading Binary File containing Strings and Numeric Data

Posted 05 September 2009 - 10:36 AM

Is this the entire file? or just part of it. I tried to determine the format of the file but found one inconsistency. This is what I have.

		Dim f As New IO.FileStream("binfile.dat", IO.FileMode.Open)
		Dim buffer(256) As Byte
		While f.Position < f.Length
			Dim len As Integer = f.ReadByte() 'Length of name
			f.Read(buffer, 0, len) 'Read Name
			Dim name As String = System.Text.Encoding.ASCII.GetChars(buffer, 0, len) 'Convert To String
			f.ReadByte() 'Space
			f.Read(buffer, 0, 7) 'Numbers are 7 digits
			Dim numstr As String = System.Text.Encoding.ASCII.GetChars(buffer, 0, 7) 'Convert To String
			Try ' second number is only 6 digits then an X. Is this an error on my part?
				Dim num As Integer = Integer.Parse(numstr) 'Convert To Integer
				MessageBox.Show(name & " " & num)
			Catch
			End Try
			f.Read(buffer, 0, 48) '48 bytes? Don't know what these are for
		End While
				f.Close()


This post has been edited by piman314: 05 September 2009 - 10:37 AM

Was This Post Helpful? 0
  • +
  • -

#6 Jogiya  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 04-September 09

Re: Reading Binary File containing Strings and Numeric Data

Posted 05 September 2009 - 10:46 AM

It was the entire file..


This loop, as you may already know, gives strange output..

Maybe BinaryReader would work?

This post has been edited by Jogiya: 05 September 2009 - 10:54 AM

Was This Post Helpful? 0
  • +
  • -

#7 piman314  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 32
  • View blog
  • Posts: 169
  • Joined: 07-August 09

Re: Reading Binary File containing Strings and Numeric Data

Posted 05 September 2009 - 10:59 AM

What is the output supposed to be?
Was This Post Helpful? 0
  • +
  • -

#8 Jogiya  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 04-September 09

Re: Reading Binary File containing Strings and Numeric Data

Posted 05 September 2009 - 11:09 AM

Something like this..


Geni Papacostas 180554X 1.0 1.0 1.0
Matt Preston 1913598 1.0 1.0 1.0
Melissa Lutton 223091X 1.0 1.0 1.0
Some Name 1200909 1.0 1.0 1.0

this data is just example..

Two Strings and some Numeric data in Decimal form..
In total there are four students/people with ID three scores in decimal form

This post has been edited by Jogiya: 05 September 2009 - 11:29 AM

Was This Post Helpful? 0
  • +
  • -

#9 piman314  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 32
  • View blog
  • Posts: 169
  • Joined: 07-August 09

Re: Reading Binary File containing Strings and Numeric Data

Posted 05 September 2009 - 12:55 PM

No matter how I read the scores in, I get either all zeroes, or a really small decimal.
		Dim f As New IO.FileStream("binfile.dat", IO.FileMode.Open)
		Dim fin As New IO.BinaryReader(f)
		Dim buffer(256) As Byte
		While f.Position < f.Length
			Dim name As String = fin.ReadString() 'Name
			fin.ReadByte() 'Space
			f.Read(buffer, 0, 7) 'Numbers are 7 digits
			Dim num As String = System.Text.Encoding.ASCII.GetString(buffer, 0, 7)
			Dim num1 As Double = fin.ReadDouble()
			Dim num2 As Double = fin.ReadDouble()
			Dim num3 As Double = fin.ReadDouble()
			f.Read(buffer, 0, 24) 'Extra 24 bytes?
			MessageBox.Show(name & " " & num & " " & num1 & " " & num2 & " " & num3)
		End While
		f.Close()


Was This Post Helpful? 1
  • +
  • -

#10 Jogiya  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 04-September 09

Re: Reading Binary File containing Strings and Numeric Data

Posted 05 September 2009 - 01:14 PM

Thankx man.. Made it work..

		Dim f As New IO.FileStream("C:\temp\ass1\binfile.dat", IO.FileMode.Open)
		Dim fin As New IO.BinaryReader(f)
		Dim buffer(256) As Byte
		While f.Position < f.Length
			Dim name As String = fin.ReadString() 'Name
			fin.ReadByte() 'Space
			f.Read(buffer, 0, 7) 'Numbers are 7 digits
			Dim num As String = System.Text.Encoding.ASCII.GetString(buffer, 0, 7)
			Dim num1 As Decimal = fin.ReadDecimal()
			Dim num2 As Decimal = fin.ReadDecimal()
			Dim num3 As Decimal = fin.ReadDecimal()
			MessageBox.Show(name & " " & num & " " & num1 & " " & num2 & " " & num3)
		End While
		f.Close()


Can you explain what happens at each step? I'm actually confused about Byte and Buffer..

Now gonna try putting them all in an array... or a text file..

This post has been edited by Jogiya: 05 September 2009 - 01:18 PM

Was This Post Helpful? 0
  • +
  • -

#11 piman314  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 32
  • View blog
  • Posts: 169
  • Joined: 07-August 09

Re: Reading Binary File containing Strings and Numeric Data

Posted 05 September 2009 - 01:32 PM

A buffer is just a place to temporarily store data in. I made a buffer of 256 bytes. I probably didn't need that many. I have it run until the position of the stream is at the end. ReadString reads in a byte for the length and then reads in the string. In the file there's a space between the name and the number, so i just read in a byte and didn't store it anywhere. Then i read in 7 bytes for the number. The bytes have the ascii character values, so i just converted them into a string. I then read in the 3 doubles. Doubles are 8 bytes, so there was an extra 24 bytes between the records. Decimals are 16 bytes, so it works perfectly (I should have tried that >.<).

This post has been edited by piman314: 05 September 2009 - 01:32 PM

Was This Post Helpful? 0
  • +
  • -

#12 Jogiya  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 04-September 09

Re: Reading Binary File containing Strings and Numeric Data

Posted 05 September 2009 - 01:46 PM

Thanks for the explaination.. really helpful..

One Last Problem!

I have made a loop that puts all the data in an array.. and all the data is perfect.. but the loop doesn't stop after reading the whole file..
it comes back up to the line

Dim name As String = fin.ReadString() 'Name
and gives error "Unable to read beyond the end of the stream."


		FileLength = FileLength + 5
		LengthIndex = LengthIndex + DecryptedFileContents.Length
		Dim f As New IO.FileStream("C:\temp\ass1\binfile.dat", IO.FileMode.Open)
		Dim fin As New IO.BinaryReader(f)
		Dim buffer(256) As Byte
		While f.Position < f.Length
			For i As Integer = 0 To f.Length - 1
				ReDim Preserve People(FileLength - 1)

				Dim name As String = fin.ReadString() 'Name
				fin.ReadByte() 'Space
				f.Read(buffer, 0, 7) 'Numbers are 7 digits

				Dim num As String = System.Text.Encoding.ASCII.GetString(buffer, 0, 7)
				Dim num1 As Decimal = fin.ReadDecimal()
				Dim num2 As Decimal = fin.ReadDecimal()
				Dim num3 As Decimal = fin.ReadDecimal()

				People(i + LengthIndex).vName = name
				People(i + LengthIndex).vID = num
				People(i + LengthIndex).Score1 = num1
				People(i + LengthIndex).Score2 = num2
				People(i + LengthIndex).Score3 = num3
				People(i + LengthIndex).Scores = num1 + num2 + num3

				PeopleCounter = PeopleCounter + 1
				MessageBox.Show(name & "," & num & "," & num1 & "," & num2 & "," & num3)

				Dim rvBW As New IO.BinaryWriter(New IO.FileStream("C:\temp\ass1\readbinfile.txt", IO.FileMode.Create))
				rvBW.Write(name & "," & num & "," & num1 & "," & num2 & "," & num3)
				rvBW.Close()

			Next
		End While
		f.Close()



Any solutions?
Was This Post Helpful? 0
  • +
  • -

#13 piman314  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 32
  • View blog
  • Posts: 169
  • Joined: 07-August 09

Re: Reading Binary File containing Strings and Numeric Data

Posted 05 September 2009 - 01:54 PM

Your for loop is trying to read in the data once for every byte in the file. try getting rid of the for loop and storing the index outside of the loop

		FileLength = FileLength + 5
		LengthIndex = LengthIndex + DecryptedFileContents.Length
		Dim f As New IO.FileStream("C:\temp\ass1\binfile.dat", IO.FileMode.Open)
		Dim fin As New IO.BinaryReader(f)
		Dim buffer(256) As Byte
		Dim i As Integer = 0
		While f.Position < f.Length
			ReDim Preserve People(FileLength - 1)

			Dim name As String = fin.ReadString() 'Name
			fin.ReadByte() 'Space
			f.Read(buffer, 0, 7) 'Numbers are 7 digits

			Dim num As String = System.Text.Encoding.ASCII.GetString(buffer, 0, 7)
			Dim num1 As Decimal = fin.ReadDecimal()
			Dim num2 As Decimal = fin.ReadDecimal()
			Dim num3 As Decimal = fin.ReadDecimal()
			People(i + LengthIndex).vName = name
			People(i + LengthIndex).vID = num
			People(i + LengthIndex).Score1 = num1
			People(i + LengthIndex).Score2 = num2
			People(i + LengthIndex).Score3 = num3
			People(i + LengthIndex).Scores = num1 + num2 + num3
			PeopleCounter = PeopleCounter + 1
			MessageBox.Show(name & "," & num & "," & num1 & "," & num2 & "," & num3)

			Dim rvBW As New IO.BinaryWriter(New IO.FileStream("C:\temp\ass1\readbinfile.txt", IO.FileMode.Create))
			rvBW.Write(name & "," & num & "," & num1 & "," & num2 & "," & num3)
			rvBW.Close()
			i += 1
		End While
		f.Close()


Was This Post Helpful? 1
  • +
  • -

#14 Jogiya  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 04-September 09

Re: Reading Binary File containing Strings and Numeric Data

Posted 05 September 2009 - 02:07 PM

It works.. thankx..

This post has been edited by Jogiya: 05 September 2009 - 02:13 PM

Was This Post Helpful? 0
  • +
  • -

#15 piman314  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 32
  • View blog
  • Posts: 169
  • Joined: 07-August 09

Re: Reading Binary File containing Strings and Numeric Data

Posted 05 September 2009 - 02:14 PM

the code I gave you isn't working? and also i noticed every time you read a new record your overwriting the last one in the output file. It would probably be more useful if you wrote them one on each line. And you only need to redim the array once. Here's the edited code:
		FileLength = FileLength + 5
		LengthIndex = LengthIndex + DecryptedFileContents.Length
		Dim f As New IO.FileStream("C:\temp\ass1\binfile.dat", IO.FileMode.Open)
		Dim fin As New IO.BinaryReader(f)
		Dim fout As New IO.StreamWriter("C:\temp\ass1\readbinfile.txt")
		Dim buffer(256) As Byte
		Dim i As Integer = 0
				ReDim Preserve People(FileLength - 1)
		While f.Position < f.Length
			Dim name As String = fin.ReadString()
			fin.ReadByte()
			f.Read(buffer, 0, 7)

			Dim num As String = System.Text.Encoding.ASCII.GetString(buffer, 0, 7)
			Dim num1 As Decimal = fin.ReadDecimal()
			Dim num2 As Decimal = fin.ReadDecimal()
			Dim num3 As Decimal = fin.ReadDecimal()
			People(i + LengthIndex).vName = name
			People(i + LengthIndex).vID = num
			People(i + LengthIndex).Score1 = num1
			People(i + LengthIndex).Score2 = num2
			People(i + LengthIndex).Score3 = num3
			People(i + LengthIndex).Scores = num1 + num2 + num3
			PeopleCounter = PeopleCounter + 1
			MessageBox.Show(name & "," & num & "," & num1 & "," & num2 & "," & num3)

			fout.WriteLine(name & "," & num & "," & num1 & "," & num2 & "," & num3)

			i += 1
		End While
		f.Close()
		fout.Close()


This post has been edited by piman314: 05 September 2009 - 02:16 PM

Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2