468,244 Members | 2,021 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,244 developers. It's quick & easy.

problem reading binary file

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()

'This call is required by the Windows Form Designer.

'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
If disposing Then
If Not (components Is Nothing) Then
End If
End If
End Sub

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

'NOTE: The following procedure is required by the Windows Form
'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
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
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.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"
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 =
Me.txtOut.Size = New System.Drawing.Size(712, 192)
Me.txtOut.TabIndex = 1
Me.txtOut.Text = "txtOut"
Me.Button_ReadFile.Location = New System.Drawing.Point(352,
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"
Me.Button_SelectFile.Location = New System.Drawing.Point(64,
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"
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(752, 273)
Me.Name = "Form1"
Me.Text = "Form1"

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"
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, _

'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 = ""

Dim iByte As Int32
iByte = 0

'reading char into a integer variable
Dim c As Integer

c = BR.ReadByte
sNextByte = c.ToString

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

'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


txtOut.Text = sOut
End Sub
End Class
Nov 20 '05 #1
2 1538
"Mad Scientist Jr" <us*************@yahoo.com> wrote in message news:7a*************************@posting.google.co m...
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
Nov 20 '05 #2
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!
Nov 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Brandon McCombs | last post: by
8 posts views Thread by shrishjain | last post: by
11 posts views Thread by Abhishek | last post: by
6 posts views Thread by arne.muller | last post: by
6 posts views Thread by efrenba | last post: by
5 posts views Thread by Neil Crighton | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by kermitthefrogpy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.