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 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
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/>
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/>
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/>
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/>
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/>
"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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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....
|
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...
|
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;
|
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
|
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))
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
| |