problem reading binary file

M

Mad Scientist Jr

i'm trying to read a file byte by byte (and later alter the data and
write it to a 2nd file byte by byte) and running into a problem where
it seems to keep reading the same byte over and over again (an endless
loop). i thought that BinaryReader.ReadByte advanced to the next byte?
i had it time out after 1000 iterations, and keeps outputting the same
byte. any help appreciated, my code is below:


Imports System.io

Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As
Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form
Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents txtOut As System.Windows.Forms.TextBox
Friend WithEvents OpenFileDialog1 As
System.Windows.Forms.OpenFileDialog
Friend WithEvents Button_ReadFile As System.Windows.Forms.Button
Friend WithEvents Button_SelectFile As System.Windows.Forms.Button
Friend WithEvents txtFile As System.Windows.Forms.TextBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.txtFile = New System.Windows.Forms.TextBox
Me.txtOut = New System.Windows.Forms.TextBox
Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog
Me.Button_ReadFile = New System.Windows.Forms.Button
Me.Button_SelectFile = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'txtFile
'
Me.txtFile.Location = New System.Drawing.Point(144, 16)
Me.txtFile.Name = "txtFile"
Me.txtFile.Size = New System.Drawing.Size(200, 20)
Me.txtFile.TabIndex = 0
Me.txtFile.Text = "txtFile"
'
'txtOut
'
Me.txtOut.Font = New System.Drawing.Font("Courier New", 8.25!,
System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,
CType(0, Byte))
Me.txtOut.Location = New System.Drawing.Point(8, 72)
Me.txtOut.Multiline = True
Me.txtOut.Name = "txtOut"
Me.txtOut.ScrollBars =
System.Windows.Forms.ScrollBars.Vertical
Me.txtOut.Size = New System.Drawing.Size(712, 192)
Me.txtOut.TabIndex = 1
Me.txtOut.Text = "txtOut"
'
'Button_ReadFile
'
Me.Button_ReadFile.Location = New System.Drawing.Point(352,
16)
Me.Button_ReadFile.Name = "Button_ReadFile"
Me.Button_ReadFile.Size = New System.Drawing.Size(64, 24)
Me.Button_ReadFile.TabIndex = 2
Me.Button_ReadFile.Text = "Read File"
'
'Button_SelectFile
'
Me.Button_SelectFile.Location = New System.Drawing.Point(64,
16)
Me.Button_SelectFile.Name = "Button_SelectFile"
Me.Button_SelectFile.Size = New System.Drawing.Size(72, 24)
Me.Button_SelectFile.TabIndex = 3
Me.Button_SelectFile.Text = "Select File"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(752, 273)
Me.Controls.Add(Me.Button_SelectFile)
Me.Controls.Add(Me.Button_ReadFile)
Me.Controls.Add(Me.txtOut)
Me.Controls.Add(Me.txtFile)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)

End Sub

#End Region

Private Sub Button_SelectFile_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button_SelectFile.Click
Dim sFileAndPath As String
OpenFileDialog1.InitialDirectory = "c:\temp"
OpenFileDialog1.Filter = "*.wav|*.wav"
OpenFileDialog1.ShowDialog()
sFileAndPath = OpenFileDialog1.FileName
If OpenFileDialog1.FileName.Length > 0 Then
txtFile.Text = OpenFileDialog1.FileName.ToString
End If
End Sub

Private Sub Button_ReadFile_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button_ReadFile.Click
Dim sOut As String

'declaring binary stream reader
Dim BR As BinaryReader

'declaring file stream object
Dim FS As FileStream

'opening Binary.Bin to read data
FS = New System.IO.FileStream(txtFile.Text, _
FileMode.Open, _
FileAccess.Read)

'instantiating binary stream reader object
BR = New System.IO.BinaryReader(FS)

'moving pointer to 0 (begininning of file)
BR.BaseStream.Seek(0, SeekOrigin.Begin)

'string for displaying bytes
Dim sNextByte As String

'clear text box
txtOut.Text = ""

' COUNT BYTES
Dim iByte As Int32
iByte = 0

'reading char into a integer variable
Dim c As Integer

' READ FIRST BYTE
c = BR.ReadByte
sNextByte = c.ToString

'looping to read the file fullest
While FS.Position < FS.Length And (iByte < 1000)
' OUTPUT [BYTE NUMBER]: [BYTE VALUE]
'sOut += "byte " & "0000".Substring(4 -
iByte.ToString.Length, iByte.ToString.Length) & iByte.ToString & ": "
& sNextByte & vbCrLf
iByte = iByte + 1
sOut += "byte " & iByte.ToString & ": " & sNextByte &
vbCrLf

' READ FIELDS AND POPULATE STRUCTURE
'p.ProdID = BR.ReadString
'p.prodDescription = BR.ReadString
'p.listPrice = BR.ReadSingle
'p.Available = BR.ReadBoolean
'p.minStock = BR.ReadInt32
c = BR.ReadByte

'c = BR.PeekChar
End While

BR.Close()
FS.Close()

txtOut.Text = sOut
End Sub
End Class
 
D

Derek Harmon

Mad Scientist Jr said:
i'm trying to read a file byte by byte (and later alter the data and
write it to a 2nd file byte by byte) and running into a problem where
it seems to keep reading the same byte over and over again

It's not reading the same byte over and over again, it's writing the same byte over and
over again. :)

Before the While loop, you read the first byte into variable 'c', and then convert it into a
string, 'sNextByte'. At the beginning of each While loop, you concatenate 'sNextByte'
onto 'sOut'. At the end of each While loop, you read the next byte into variable 'c'.

Notice that 'sNextByte' is only assigned to once, before the While loop, and it's all
that you ever save into 'sOut'. You're ignoring all of the subsequent reads into 'c'.


Derek Harmon
 
M

Mad Scientist Jr

gawd, you had to tell me THAT?
thanks a lot though, this was a maddening error.
i wish i could have troubled you with something a little more substantial!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top