Binary files?

  • Thread starter Thread starter Davej
  • Start date Start date
D

Davej

In vb.net express 2008 what are the best methods for reading a binary
file? I am doing something wrong with BinaryReader() because I get an
exception a short distance into the file. Thanks.
 
Davej was thinking very hard :
In vb.net express 2008 what are the best methods for reading a binary
file? I am doing something wrong with BinaryReader() because I get an
exception a short distance into the file. Thanks.

It's almost impossible to answer this question as written. You provide
not information about what type of exception it is - for example some
methods such as ReadBytes/ReadChars, are documented to throw an
ArgumentException when the bytes represent a surrogate pair.

You also provide no source code. If you would like help on this issue
I suggest you provide a short but complete working example that
demonstrates the issue. I also suggest you provide some information
about the type of data in the file.

HTH
 
Davej was thinking very hard :


It's almost impossible to answer this question as written.  You provide
not information about what type of exception it is - for example some
methods such as ReadBytes/ReadChars, are documented to throw an
ArgumentException when the bytes represent a surrogate pair.

This was using "Visual Basic 2008 Express." The code seems to read a
short distance into the file and then bomb out at as if it cannot
handle a particular character. What am I doing wrong?
'System.ArgumentException' occurred in mscorlib.dll Thanks.

Code I've tried;

Try
Dim br As New BinaryReader(New FileStream(path, FileMode.Open,
FileAccess.Read))

Do While br.PeekChar <> -1
c = br.ReadChar()

filesize += 1
Loop
MsgBox("filesize=" & filesize.ToString)
br.Close()

Catch ex As Exception
MsgBox("Error opening or reading file at filepos=" &
filesize.ToString)
Exit Sub
End Try

And also...

Dim fs As FileStream = New FileStream(path, FileMode.Open,
FileAccess.Read)
Dim br As BinaryReader = New BinaryReader(fs)

Do While br.PeekChar <> -1
b = br.ReadByte()

etc...
 
Hi
In vb.net express 2008 what are the best methods for reading a binary
file? I am doing something wrong with BinaryReader() because I get an
exception a short distance into the file. Thanks.

I would suggest starting with your code as a starting point. What is the
error you get and on which line does it happen ? Have you tried standard
techniques such as stepping through the code to see what happens ?
 
Davej explained :
This was using "Visual Basic 2008 Express." The code seems to read a
short distance into the file and then bomb out at as if it cannot
handle a particular character. What am I doing wrong?
'System.ArgumentException' occurred in mscorlib.dll Thanks.

Code I've tried;

Try
Dim br As New BinaryReader(New FileStream(path, FileMode.Open,
FileAccess.Read))

Do While br.PeekChar <> -1
c = br.ReadChar()

filesize += 1
Loop
MsgBox("filesize=" & filesize.ToString)
br.Close()

Catch ex As Exception
MsgBox("Error opening or reading file at filepos=" &
filesize.ToString)
Exit Sub
End Try

And also...

Dim fs As FileStream = New FileStream(path, FileMode.Open,
FileAccess.Read)
Dim br As BinaryReader = New BinaryReader(fs)

Do While br.PeekChar <> -1
b = br.ReadByte()

etc...

You still did not provide information about the file... What kind of
content is it? Further - what line does the exception occure on?
Also, you said your getting an ArgumentException - that is documented
to be thrown when the bytes or char represents a unicode surrogate
pair.

More info neede.
 
Hello Tom,

if it is just the file size you want,
this can be done much easier and quicker by using
the FileInfo object.

fileSize = New System.IO.FileInfo(path).Length

Best regards,

Martin
 
You still did not provide information about the file...  What kind of
content is it?  Further - what line does the exception occur on?  
Also, you said your getting an ArgumentException - that is documented
to be thrown when the bytes or char represents a unicode surrogate
pair.

Oh, I want to be able to read any file. I am old, from the ancient
times when a binary reader reading byte by byte could read any file.
Thanks.
 
Am 14.05.2010 15:33, schrieb Davej:
Oh, I want to be able to read any file. I am old, from the ancient
times when a binary reader reading byte by byte could read any file.
Thanks.

In general, you can use the BinaryReader.
But you have a specific problem. Therefore more specific information is helpful,
like the line of the exception. Without knowing the type of file,
we can not know if ReadChar can work well. It depends on the file format
and the character encoding used. A char is not a byte. If you want to read
byte by byte, use the ReadByte method instead. Or the Read(byte(), ...)
method to read into an array of bytes.
 
Hi,
Oh, I want to be able to read any file. I am old, from the ancient
times when a binary reader reading byte by byte could read any file.
Thanks.

Still you don't tell where it happens as asked by Tom.

My guess is that it happens at PeekChar. If you check
http://msdn.microsoft.com/en-us/library/system.io.binaryreader.peekchar.aspx
you'll see that ArgumentException happens when the char is not valid with
the current encoding. The trick is that BinaryReader is not "just" a binary
reader. It exposes an underlying stream as a binary stream but still use
some encoding( UTF8 by default).

If all you need is to read bytes does it work if you just use FileStream ?
Else you could pass the encoding used by your file as a parameter when
building the BinaryReader (Text.Encoding.Default ?).

The smallest code sample that repro the problem could also help (here likely
with a first section that writes the file). For example :
Sub Main()
Const Path As String = "Test.txt"
' Write
Dim fs As New FileStream(Path, FileMode.Create)
Dim bw As New BinaryWriter(fs)
For i As Integer = 0 To 1000
bw.Write(i)
Next
bw.Close()
fs.Dispose()
' Read
fs = New FileStream(Path, FileMode.Open)
Dim br As New BinaryReader(fs)
Do While br.PeekChar <> -1 ' <====== FAILS HERE
Debug.WriteLine(br.ReadChar)
Loop
br.Close()
fs.Dispose()
End Sub

If you use Dim br as New BinaryReader(fs,Text.Encoding.Default) it works. In
the first version, a value is written that is not valid UT8 character so it
fails later when using PeekChar. With the Text.Encoding.Default (which is
ANSI), all character values are valid and the problem goes away.
 
--
--
You still did not provide information about the file... What kind of
content is it? Further - what line does the exception occur on?
Also, you said your getting an ArgumentException - that is documented
to be thrown when the bytes or char represents a unicode surrogate
pair.

Oh, I want to be able to read any file. I am old, from the ancient
times when a binary reader reading byte by byte could read any file.
Thanks.
:)

Maybe this helps?
http://msdn.microsoft.com/en-us/library/system.io.binaryreader.readbytes.aspx

It refers to a ReadByte method. Your code seems to be
reading characters. Oddly, though, the link above also
mentions the ArgumentException error in connection with
surrogate pairs, even in the context of bytes. It mentions
a "unicode decoder". I don't know whether that's a misprint
or whether .Net truly can't just read bytes without any funny
business.

I'm no .Net expert, but you seem to be getting answers
to everything but the question you asked, so I too pity.
 
After serious thinking Mayayana wrote :
--
--


Oh, I want to be able to read any file. I am old, from the ancient
times when a binary reader reading byte by byte could read any file.
Thanks.

:)

Maybe this helps?
http://msdn.microsoft.com/en-us/library/system.io.binaryreader.readbytes.aspx

It refers to a ReadByte method. Your code seems to be
reading characters. Oddly, though, the link above also
mentions the ArgumentException error in connection with
surrogate pairs, even in the context of bytes. It mentions
a "unicode decoder". I don't know whether that's a misprint
or whether .Net truly can't just read bytes without any funny
business.

I'm no .Net expert, but you seem to be getting answers
to everything but the question you asked, so I too pity.

Well, there is reason I've not been answering. I've been trying to get
this guy to learn how to post a question that can be answered. Showing
up and saying "Hey, BinaryReader is throwing and exception, help!"
doesn't do the op or those of us trying to help any good at all...

I expect, like Patrice, that the exception is actually being thrown on
the PeekChars method. But, the op has thus far been reluctant to
provide clear information about the exception, what line it occurs on,
the nature of the data, and what he is actually trying to accomplish.
From the small snipit of non-working code it appears he is trying to
get the file length - but, I in fact htink that he is just
experiementing reading the bytes of a file. In that case, he should
just use the FileStream directly without the BinaryReader. Since the
FileStream will be a true stream of bytes. The OP is not using the
BinaryReader correctly, which is why he is getting the exception...
 
My guess is that it happens at PeekChar. If you checkhttp://msdn.microsoft.com/en-us/library/system.io.binaryreader.peekch...
you'll see that ArgumentException happens when the char is not valid with
the current encoding. The trick is that BinaryReader is not "just" a binary
reader. It exposes an underlying stream as a binary stream but still use
some encoding( UTF8 by default).

If all you need is to read bytes does it work if you just use FileStream ?

You are right. I used BinaryReader only because that is what the
Murach's textbook shows in the binary file i/o example. It seems that
FileStream is better off without it, or at least the code now runs
without generating an exception and seems to produce results that
suggest it is working.
 
Back
Top