How to write bytes into array, into specific location vb.net
How to write bytes into array, into specific location vb.net
I'm reading bytes from the file, into a byte array
Dim input As New FileStream("scm_app.bin", FileMode.Open)
Dim bytes() As Byte
bytes = reader.ReadBytes(CInt(input.Length))
But how to indicate that I want to start reading into an array from the fifth index?
bytes(5) = reader.ReadBytes(CInt(input.Length))
1 Answer
1
That code is rather screwy. If you just want to read Bytes
from a file then you don't need a BinaryReader
. The FileStream
can read Bytes
for you. Normally you wouldn't even need that as you can read a whole file into an array like this:
Bytes
BinaryReader
FileStream
Bytes
Dim data = IO.File.ReadAllBytes(filePath)
If you want to read into a specific position in an existing array though, you can use the FileStream
like this:
FileStream
Dim data As Byte() 'The array to write the data to.
Dim startPosition As Integer 'The position in the array at which to start writing the data.
Dim filePath As String
Using fs = IO.File.OpenRead(filePath)
fs.Read(data, startPosition, CInt(fs.Length))
End Using
You need to make up your mind what question you want answered. I've answered the question that you asked. If you actually want the answer to a different question then you should have asked that question. Unless you have a good reason not to, you should accept this answer based on the question you asked. If you have a different question, start a new thread and ask that question there.
– jmcilhinney
Sep 12 '18 at 6:21
yes, you are right
– James_BK
Sep 12 '18 at 6:23
In my answer,
data
is the array to write the data from the fil into. If your array is bytes
then use bytes
instead of data
. In my answer, startPosition
is the position to start writing the data. If you want to start writing the data at index 5 then you set startPosition
to 5. Just as you did in your original code, I specified that the entire file should be read but, if you only want to read a specific number of bytes then specify that number instead of the Length
of the FileStream
.– jmcilhinney
Sep 12 '18 at 6:24
data
bytes
bytes
data
startPosition
startPosition
Length
FileStream
@jm im clearly understand what did you mean in your answer, but i wanted to ask how to deal with 2 arrays when i want to copy the content of 1 to another but in a different index. your answer doesn't fit because i can't do "fs.Read(data, startPosition, CInt(fs.Length))" with an array, i can use it only with IO.FILE. But again it was my fault, ive asked the question so poorly
– James_BK
Sep 12 '18 at 6:47
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Thx for your reply jm, but if i need to take 255 bytes from the huge array and put them into a smaller array which is 261byte length, but those 255 should start from the fifth index, how can i do that?
– James_BK
Sep 12 '18 at 6:17