Binary files?

  • Follow


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.
0
Reply Davej 5/13/2010 5:09:24 PM

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


0
Reply Tom 5/13/2010 5:53:21 PM


On May 13, 12:53=A0pm, 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. =A0You 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 =3D br.ReadChar()

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

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

And also...

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

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

  etc...
0
Reply Davej 5/13/2010 6:42:25 PM

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 

0
Reply Patrice 5/13/2010 6:46:53 PM

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


0
Reply Tom 5/13/2010 7:19:21 PM

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
0
Reply Martin 5/13/2010 9:25:36 PM

On May 13, 2:19=A0pm, Tom Shelton <tom_shel...@comcast.invalid> wrote:
>
> You still did not provide information about the file... =A0What kind of
> content is it? =A0Further - what line does the exception occur on? =A0
> 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.
0
Reply Davej 5/14/2010 1:33:36 PM

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
0
Reply Armin 5/14/2010 2:12:43 PM

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.

--
Patrice




0
Reply Patrice 5/14/2010 2:36:19 PM


-- 
--
"Davej" <galt_57@hotmail.com> wrote in message 
news:50bb2763-325a-40db-aca2-d531f91e2244@e1g2000yqe.googlegroups.com...
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/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. 


0
Reply Mayayana 5/14/2010 2:36:34 PM

After serious thinking Mayayana wrote :
>
> -- 
> --
> "Davej" <galt_57@hotmail.com> wrote in message 
> news:50bb2763-325a-40db-aca2-d531f91e2244@e1g2000yqe.googlegroups.com...
> 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/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...

-- 
Tom Shelton


0
Reply Tom 5/14/2010 4:05:31 PM

On May 14, 9:36=A0am, "Patrice" <http://scribe-en.blogspot.com/> wrote:
>
> My guess is that it happens at PeekChar. If you checkhttp://msdn.microsof=
t.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 bina=
ry
> 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.
0
Reply Davej 5/16/2010 6:04:00 AM

11 Replies
183 Views

(page loaded in 0.197 seconds)

8/11/2012 3:04:59 PM


Reply: