PC Review


Reply
Thread Tools Rate Thread

Binary files?

 
 
Davej
Guest
Posts: n/a
 
      13th May 2010
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.
 
Reply With Quote
 
 
 
 
Tom Shelton
Guest
Posts: n/a
 
      13th May 2010
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


 
Reply With Quote
 
Davej
Guest
Posts: n/a
 
      13th May 2010
On May 13, 12:53*pm, Tom Shelton <tom_shel...@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...
 
Reply With Quote
 
Patrice
Guest
Posts: n/a
 
      13th May 2010

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

 
Reply With Quote
 
Tom Shelton
Guest
Posts: n/a
 
      13th May 2010
Davej explained :
> On May 13, 12:53*pm, Tom Shelton <tom_shel...@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


 
Reply With Quote
 
Martin H.
Guest
Posts: n/a
 
      13th May 2010
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
 
Reply With Quote
 
Davej
Guest
Posts: n/a
 
      14th May 2010
On May 13, 2:19*pm, Tom Shelton <tom_shel...@comcast.invalid> wrote:
>
> 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.
 
Reply With Quote
 
Armin Zingler
Guest
Posts: n/a
 
      14th May 2010
Am 14.05.2010 15:33, schrieb Davej:
> On May 13, 2:19 pm, Tom Shelton <tom_shel...@comcast.invalid> wrote:
>>
>> 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.


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.


--
Armin
 
Reply With Quote
 
Patrice
Guest
Posts: n/a
 
      14th May 2010
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/libr....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.

--
Patrice




 
Reply With Quote
 
Mayayana
Guest
Posts: n/a
 
      14th May 2010


--
--
"Davej" <(E-Mail Removed)> wrote in message
news:50bb2763-325a-40db-aca2-(E-Mail Removed)...
On May 13, 2:19 pm, Tom Shelton <tom_shel...@comcast.invalid> wrote:
>
> 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/libr...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.


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Binary files bartolo19635@gmail.com DIY PC 1 25th Jul 2007 09:20 PM
Binary files kate2006 Microsoft Dot NET 6 28th Feb 2006 08:13 AM
vb6 put,get for binary files in .Net =?Utf-8?B?bWlrZSB3Lg==?= Microsoft VB .NET 2 3rd Mar 2005 06:29 PM
Binary Files Jenny Microsoft Excel Programming 1 20th Sep 2004 08:42 PM
Re: binary files inon zukerman Microsoft C# .NET 0 8th Jul 2003 10:54 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:45 AM.