CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2023
    Posts
    2

    New To Using FileStream Can Someone Explain It? I Think My Code Is Overkill.

    Hello,

    I am new to using the FileStream class. I have the following code, but do not fully understand it:

    Code:
    Using myFile As New FileStream(myPath, FileMode.Open, FileAccess.Read)
        Try
    
            Dim myChunk = 999
            Dim myFileLen As Integer = CInt(myFile.Length)
            Dim myBuff(myFileLen) as Byte
            Dim myBlock(myChunk) as Byte
            Dim myCount As Integer = -1
            Dim myPos As Integer = 256
    
            While myCount <> 0
                myCount = myFile.Read(myBlock, 0, myChunk-1)
                Array.Copy(myBlock, 0, myBuff, myPos, myCount)
                myPos = myPos + myCount
            End While
    
            For Each myElement As Integer In myBuff
                Console.WriteLine(myElement)
            Next
    
        End Try
    End Using
    I have a binary file that contains a lot of 8 bit Bytes (0-255) and no words. I was just looking for a simple way to read the file from point x and read n number of Bytes.

    For example starting at Byte 256, read 999 Bytes from the file.

    Do you have to read them into an array when using FileStream?

    It seems like I have a lot of variables for what I am trying to do. Can I simplify it?

    My file is 35MB in size. Not huge, but I'd rather deal with chunks of data and be able to start the chunk at any position in the file. I'd also rather load each Byte one at time, convert to whatever and display it or do whatever to it. Then if I need to, load another chunk.

    Thank you.

  2. #2
    Join Date
    Nov 2023
    Posts
    2

    Re: New To Using FileStream Can Someone Explain It? I Think My Code Is Overkill.

    I am using VB.net with Visual Studio 2019.

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,861

    Re: New To Using FileStream Can Someone Explain It? I Think My Code Is Overkill.

    You might get more responses if you post to this sites' sister site VBForums
    https://www.vbforums.com/forumdispla...sual-Basic-NET
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured