473,735 Members | 8,833 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 37114
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("myf ile.bin", FileMode.Open)
Dim bytes(CInt(inpu t.Length - 1)) As Byte
input.Read(byte s, 0, CInt(input.Leng th))

' with a BinaryReader
Dim input As New FileStream("myf ile.bin", FileMode.Open)
Dim reader As New BinaryReader(in put)
Dim bytes() As Byte
bytes = reader.ReadByte s(CInt(input.Le ngth))

Hope this helps
Jay
"S Shulman" <sm*******@hotm ail.com> wrote in message
news:OY******** ******@TK2MSFTN GP11.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("myf ile.bin", FileMode.Open)
Dim bytes(CInt(inpu t.Length - 1)) As Byte
input.Read(byte s, 0, CInt(input.Leng th))


Attention!

<URL:http://www.yoda.arachs ys.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("myf ile.bin", FileMode.Open)
Dim bytes(CInt(inpu t.Length - 1)) As Byte
input.Read(byte s, 0, CInt(input.Leng th))


Attention!

<URL:http://www.yoda.arachs ys.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(myfi lename, 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("myf ile.bin", FileMode.Open)
Dim bytes(CInt(inpu t.Length - 1)) As Byte
input.Read(byte s, 0, CInt(input.Leng th))


Attention!

<URL:http://www.yoda.arachs ys.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.Re adBytes 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****@discuss ions.microsoft. com> wrote in message
news:26******** *************** ***********@mic rosoft.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(myfi lename, 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("myf ile.bin", FileMode.Open)
> Dim bytes(CInt(inpu t.Length - 1)) As Byte
> input.Read(byte s, 0, CInt(input.Leng th))

Attention!

<URL:http://www.yoda.arachs ys.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******** *****@TK2MSFTNG P11.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
35622
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. Thanks Phil
3
2974
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 might, I can't get binary to work (Word b0rks trying to open the resulting file). When I create a simple RTF in Wordpad and then view its source, Wordpad seems to be embedding the image as hex. Does anyone have a suggestion as to how I could read an...
2
268
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
2274
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
3496
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
1894
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 consists of 9 variables of Integer: Signed 32-bit integer (4 bytes) and 8 variables of Double: Signed 64-bit IEEE double-precision floating point number (8 bytes). when i tried to assign integer and double data type for these variables i am...
2
2076
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 of the file of the binary file..... Any suggestions
0
8962
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8785
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9251
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6747
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6049
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4559
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3273
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2739
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2188
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.