473,763 Members | 6,149 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Re adByte 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.
InitializeCompo nent()

'Add any initialization after the InitializeCompo nent() 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.Disp ose()
End If
End If
MyBase.Dispose( disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.Componen tModel.IContain er

'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.OpenFileD ialog
Friend WithEvents Button_ReadFile As System.Windows. Forms.Button
Friend WithEvents Button_SelectFi le As System.Windows. Forms.Button
Friend WithEvents txtFile As System.Windows. Forms.TextBox
<System.Diagnos tics.DebuggerSt epThrough()> Private Sub
InitializeCompo nent()
Me.txtFile = New System.Windows. Forms.TextBox
Me.txtOut = New System.Windows. Forms.TextBox
Me.OpenFileDial og1 = New System.Windows. Forms.OpenFileD ialog
Me.Button_ReadF ile = New System.Windows. Forms.Button
Me.Button_Selec tFile = New System.Windows. Forms.Button
Me.SuspendLayou t()
'
'txtFile
'
Me.txtFile.Loca tion = New System.Drawing. Point(144, 16)
Me.txtFile.Name = "txtFile"
Me.txtFile.Size = New System.Drawing. Size(200, 20)
Me.txtFile.TabI ndex = 0
Me.txtFile.Text = "txtFile"
'
'txtOut
'
Me.txtOut.Font = New System.Drawing. Font("Courier New", 8.25!,
System.Drawing. FontStyle.Regul ar, System.Drawing. GraphicsUnit.Po int,
CType(0, Byte))
Me.txtOut.Locat ion = New System.Drawing. Point(8, 72)
Me.txtOut.Multi line = True
Me.txtOut.Name = "txtOut"
Me.txtOut.Scrol lBars =
System.Windows. Forms.ScrollBar s.Vertical
Me.txtOut.Size = New System.Drawing. Size(712, 192)
Me.txtOut.TabIn dex = 1
Me.txtOut.Text = "txtOut"
'
'Button_ReadFil e
'
Me.Button_ReadF ile.Location = New System.Drawing. Point(352,
16)
Me.Button_ReadF ile.Name = "Button_ReadFil e"
Me.Button_ReadF ile.Size = New System.Drawing. Size(64, 24)
Me.Button_ReadF ile.TabIndex = 2
Me.Button_ReadF ile.Text = "Read File"
'
'Button_SelectF ile
'
Me.Button_Selec tFile.Location = New System.Drawing. Point(64,
16)
Me.Button_Selec tFile.Name = "Button_SelectF ile"
Me.Button_Selec tFile.Size = New System.Drawing. Size(72, 24)
Me.Button_Selec tFile.TabIndex = 3
Me.Button_Selec tFile.Text = "Select File"
'
'Form1
'
Me.AutoScaleBas eSize = New System.Drawing. Size(5, 13)
Me.ClientSize = New System.Drawing. Size(752, 273)
Me.Controls.Add (Me.Button_Sele ctFile)
Me.Controls.Add (Me.Button_Read File)
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_SelectFi le_Click(ByVal sender As System.Object,
ByVal e As System.EventArg s) Handles Button_SelectFi le.Click
Dim sFileAndPath As String
OpenFileDialog1 .InitialDirecto ry = "c:\temp"
OpenFileDialog1 .Filter = "*.wav|*.wa v"
OpenFileDialog1 .ShowDialog()
sFileAndPath = OpenFileDialog1 .FileName
If OpenFileDialog1 .FileName.Lengt h > 0 Then
txtFile.Text = OpenFileDialog1 .FileName.ToStr ing
End If
End Sub

Private Sub Button_ReadFile _Click(ByVal sender As System.Object,
ByVal e As System.EventArg s) 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.FileS tream(txtFile.T ext, _
FileMode.Open, _
FileAccess.Read )

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

'moving pointer to 0 (begininning of file)
BR.BaseStream.S eek(0, SeekOrigin.Begi n)

'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".Substrin g(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.prodDescript ion = 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
Nov 20 '05 #1
2 1863
"Mad Scientist Jr" <us************ *@yahoo.com> wrote in message news:7a******** *************** **@posting.goog le.com...
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
9855
by: Brandon McCombs | last post by:
This may be the wrong group but I didn't see anything for VC++ so I'm trying here. I have a C++ book by Deitel and Deitel that says I can use fstream File("data.dat", ios::in | ios::out | ios::binary) to declare a file object with read/write modes turned on for working with binary data. I've tried this and my file is not created. The only time it is created is when I specify ifstream or ofstream but not fstream. I've tried removing the...
6
519
by: | last post by:
I am rewriting a C++ application in C#. This file has a combination of Text and Binary data. I used CFile before to read the text. If I hit a certain string that denotes the following data is binary, I used the current position in the file and another stream to read to the binary data. All text data is ended with a carriage return / line feed while the binary is actually an image file listed byte by byte. Preceding the binary data...
8
1585
by: shrishjain | last post by:
Dear All, I am having problem in reading bytes from a binary file. I read the file in following way: ifstream in("filename"); char c; while(true) { in.read(&c, 1); if(in.eof()){
20
3072
by: ishmael4 | last post by:
hello everyone! i have a problem with reading from binary file. i was googling and searching, but i just cant understand, why isnt this code working. i could use any help. here's the source code: --cut here-- typedef struct pkg_ { short int info; char* data;
11
6639
by: Abhishek | last post by:
I have a problem transfering files using sockets from pocket pc(.net compact c#) to desktop(not using .net just mfc and sockets 2 API). The socket communication is not a issue and I am able to transfer data across.On the serve I am using Socket 2 API (recv function to read bytes)and not using ..NET. I use FileStream to open the file on the pocket pc, then associate a BinaryReader object with the stream and call ReadBytes to read all the...
6
5272
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
6
3529
by: efrenba | last post by:
Hi, I came from delphi world and now I'm doing my first steps in C++. I'm using C++builder because its ide is like delphi although I'm trying to avoid the vcl. I need to insert new features to an old program that I wrote in delphi and it's a good opportunity to start with c++.
5
5165
by: Neil Crighton | last post by:
I'm using the zipfile library to read a zip file in Windows, and it seems to be adding too many newlines to extracted files. I've found that for extracted text-encoded files, removing all instances of '\r' in the extracted file seems to fix the problem, but I can't find an easy solution for binary files. The code I'm using is something like: from zipfile import Zipfile z = Zipfile(open('zippedfile.zip'))
27
5122
by: Jeff | last post by:
Im trying to figure out why I cant read back a binary file correctly. I have the following union: #define BITE_RECORD_LEN 12 typedef union { unsigned char byte; struct { unsigned char type; /* 0 Type */ unsigned char sn; /* 1-3 Serial Number */
0
9386
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9997
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9937
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9822
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8821
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5270
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2793
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.