Read Binary File

  • Thread starter Thread starter S Shulman
  • Start date Start date
S

S Shulman

Hi All

I need to read a file to an array of bytes (any type of file)
I tried using FileStream and BinaryReader but it doesn't seem to work

Thank you,
Shmuel
 
Shmuel,
You don't really need the BinaryReader if you simply want to read the entire
file into a byte array.

Either of the following works.

' without a BinaryReader
Dim input As New FileStream("myfile.bin", FileMode.Open)
Dim bytes(CInt(input.Length - 1)) As Byte
input.Read(bytes, 0, CInt(input.Length))

' with a BinaryReader
Dim input As New FileStream("myfile.bin", FileMode.Open)
Dim reader As New BinaryReader(input)
Dim bytes() As Byte
bytes = reader.ReadBytes(CInt(input.Length))

Hope this helps
Jay
 
Herfried,
Attention!

Careful! ;-)

I was thinking of mentioning you really should "block" the reads, but didn't
consider it important enough in my initial answer...
M S Herfried K. Wagner

BTW: What is the "M S" in your signature? I'm on the Marklin Mailing List,
and MS refers to Mobile Station on there, not Microsoft...

Jay
 
Jay,

* "Jay B. Harlow said:
Careful! ;-)

I was thinking of mentioning you really should "block" the reads, but didn't
consider it important enough in my initial answer...
ACK.


BTW: What is the "M S" in your signature? I'm on the Marklin Mailing List,
and MS refers to Mobile Station on there, not Microsoft...

;-) -- it stands for "Microsoft". "MSFT" would not fit into the signature.
 
Just learning VB.Net and was interested in the blocking you mentioned. If I
understand correctly, the following is not guaranteed to read all the bytes:

dim fs as FileStream(myfilename, filemode.Open)
dim byt() as byte
dim r as binaryreader(fs)
byt = r.ReadBytes(fs.Length)

Do I need to use blocking to insure I get all the file into byt? I didn't
see this mentioned in the VB.Net Documentation on the binary reader class.

Jay B. Harlow said:
Herfried,
Attention!

Careful! ;-)

I was thinking of mentioning you really should "block" the reads, but didn't
consider it important enough in my initial answer...
M S Herfried K. Wagner

BTW: What is the "M S" in your signature? I'm on the Marklin Mailing List,
and MS refers to Mobile Station on there, not Microsoft...

Jay
 
Dennis,
Reading Jon's site (the link Herfried gave) you should block (read in
chunks) the read, when using Stream.Read, at the very least you should check
the return value from Stream.Read to ensure that you read what you expected
to read!


BinaryReader.ReadBytes does not allow you to block the read, I would expect
internally it uses code similar to Jon's code.


Hope this helps
Jay


Dennis said:
Just learning VB.Net and was interested in the blocking you mentioned. If I
understand correctly, the following is not guaranteed to read all the bytes:

dim fs as FileStream(myfilename, filemode.Open)
dim byt() as byte
dim r as binaryreader(fs)
byt = r.ReadBytes(fs.Length)

Do I need to use blocking to insure I get all the file into byt? I didn't
see this mentioned in the VB.Net Documentation on the binary reader class.

Jay B. Harlow said:
Herfried,
Attention!

Careful! ;-)

I was thinking of mentioning you really should "block" the reads, but didn't
consider it important enough in my initial answer...
M S Herfried K. Wagner

BTW: What is the "M S" in your signature? I'm on the Marklin Mailing List,
and MS refers to Mobile Station on there, not Microsoft...

Jay


Herfried K. Wagner said:
Jay,

* "Jay B. Harlow [MVP - Outlook]" <[email protected]> scripsit:
Either of the following works.

' without a BinaryReader
Dim input As New FileStream("myfile.bin", FileMode.Open)
Dim bytes(CInt(input.Length - 1)) As Byte
input.Read(bytes, 0, CInt(input.Length))

Attention!

<URL:http://www.yoda.arachsys.com/csharp/readbinary.html>
 
BTW: What is the "M S" in your signature? I'm on the Marklin Mailing List,
and MS refers to Mobile Station on there, not Microsoft...

Look at the left-hand side of all three lines of his .sig:

In case you're still not getting it, try this:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top