473,394 Members | 1,642 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Read Binary File

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
Nov 21 '05 #1
8 37042
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
"S Shulman" <sm*******@hotmail.com> wrote in message
news:OY**************@TK2MSFTNGP11.phx.gbl...
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

Nov 21 '05 #2
Jay,

* "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> 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>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #3
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 [MVP]" <hi***************@gmx.at> wrote in message
news:2o************@uni-berlin.de... Jay,

* "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> 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>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #4
Jay,

* "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> scripsit:
Attention!


Careful! ;-)

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


ACK.
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...


;-) -- it stands for "Microsoft". "MSFT" would not fit into the signature.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #5
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 [MVP - Outlook]" wrote:
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 [MVP]" <hi***************@gmx.at> wrote in message
news:2o************@uni-berlin.de...
Jay,

* "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> 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>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>


Nov 21 '05 #6
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" <De****@discussions.microsoft.com> wrote in message
news:26**********************************@microsof t.com...
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 [MVP - Outlook]" wrote:
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 [MVP]" <hi***************@gmx.at> wrote in message
news:2o************@uni-berlin.de...
Jay,

* "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> 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>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>


Nov 21 '05 #7

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:O6*************@TK2MSFTNGP11.phx.gbl...
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...


Look at the left-hand side of all three lines of his .sig:
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
In case you're still not getting it, try this:
M S
M V P
V B

Nov 21 '05 #8
Jeff,

You are right this is definitly something what is Herfried,

http://www.mmv.co.jp/products/

:-)

Cor
Nov 21 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Phil Powell | last post by:
I have a PHP script that would read in a binary file and display it as if it were <img src>, how would you do that w/o changing the header's MIME type? The entire file does not need to be changed....
3
by: DrewM | last post by:
I'm using ASP to generate an RTF (rich text) file, via the FSO. No problems with text, but when it comes to inserting images, the files have to be embedded as either binary or hex. Try as I...
2
by: Pete | last post by:
Hi Could someone kindly help with the C# equivilent of the following 4 lines of C code. I'm *really* struggling with this. ( Colours.dat contains 300 RGB values ) COLORREF Colours;
1
by: Quinn | last post by:
Hi all, I have some binary files in the following format: text line 1 text line 2 .... text line N end of text single in binary 1 single in binary 2 single N EOF
4
by: Matrixinline | last post by:
Hi All Here is my problem I am using a Unicode project and I tried to read the File like sPath = LPCTSTR; FILE* oFp = _tfopen(sPath,L"r"); while(!feof(oFp))
2
by: kowndinya | last post by:
Hello, i want to read values from a binary file in VisualBasic 6.0 Initially i want to read only header of this binary file. I know this structure for this header. It is 100 bytes long and...
2
by: tulasisridhar | last post by:
Hi All, I have binary file created in Unix I now need to read it using VB.net and doo some procesing. There is some conversion between bigendian and little endians. I know the structure...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.