From: Davej on
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.
From: Tom Shelton on
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

--
Tom Shelton


From: Davej on
On May 13, 12:53 pm, Tom Shelton <tom_shel...(a)comcast.invalid> wrote:
> 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.
>

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...
From: Patrice on

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 ?

--
Patrice

From: Tom Shelton on
Davej explained :
> On May 13, 12:53�pm, Tom Shelton <tom_shel...(a)comcast.invalid> wrote:
>> 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.
>>
>
> 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.

--
Tom Shelton