Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8058699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T09:26:27+00:00 2026-06-05T09:26:27+00:00

I’m new to programming in general (My understanding of programming concepts is still growing.).

  • 0

I’m new to programming in general (My understanding of programming concepts is still growing.). So this question is about learning, so please provide enough info for me to learn but not so much that I can’t, thank you.
(I would also like input on how to make the code reusable with in the project.)

The goal of the project I’m working on consists of:

  1. Read binary file.

  2. I have known offsets I need to read to find a particular chunk of data from within this file.

  3. First offset is first 4 bytes(Offset for end of my chunk).

  4. Second offset is 16 bytes from end of file. I read for 4 bytes.(Gives size of chunk in hex).

  5. Third offset is the 4 bytes following previous, read for 4 bytes(Offset for start of chunk in hex).

  6. Locate parts in the chunk to modify by searching ASCII text as well as offsets.

Now I have the start offset, end offset and size of my chunk.
This should allow me to read bytes from file into a byte array and know the size of the array ahead of time.

(Questions: 1. Is knowing the size important? Other than verification. 2. Is reading part of a file into a byte array in order to change bytes and overwrite that part of the file the best method?)

So far I have managed to read the offsets from the file using BinaryReader on a MemoryStream. I then locate the chunk of data I need and read that into a byte array.

I’m stuck in several ways:

  • What are the best practices for binary Reading / Writing?
  • What’s the best storage convention for the data that is read?
  • When I need to modify bytes how do I go about that.
  • Should I be using FileStream?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T09:26:27+00:00Added an answer on June 5, 2026 at 9:26 am

    Since you want to both read and write, it makes sense to use the FileStream class directly (using FileMode.Open and FileAccess.ReadWrite). See FileStream on MSDN for a good overall example.

    1. You do need to know the number of bytes that you are going to be reading from the stream. See the FileStream.Read documentation.
    2. Fundamentally, you have to read the bytes into memory at some point if you’re going to use and later modify their contents. So you will have to make an in-memory copy (using the Read method is the right way to go if you’re reading a variable-length chunk at a time).

    As for best practices, always dispose your streams when you’re done; e.g.:

    using (var stream = File.Open(FILE_NAME, FileMode.Open, FileAccess.ReadWrite))
    {
        //Do work with the FileStream here.
    }
    

    If you’re going to do a large amount of work, you should be doing the work asynchronously. (Let us know if that’s the case.)

    And, of course, check the FileStream.Read documentation and also the FileStream.Write documentation before using those methods.

    Reading bytes is best done by pre-allocating an in-memory array of bytes with the length that you’re going to read, then reading those bytes. The following will read the chunk of bytes that you’re interested in, let you do work on it, and then replace the original contents (assuming the length of the chunk hasn’t changed):

    EDIT: I’ve added a helper method to do work on the chunk, per the comments on variable scope.

    using (var stream = File.Open(FILE_NAME, FileMode.Open, FileAccess.ReadWrite))
    {
        var chunk = new byte[numOfBytesInChunk];
        var offsetOfChunkInFile = stream.Position; // It sounds like you've already calculated this.
        stream.Read(chunk, 0, numOfBytesInChunk);
    
        DoWorkOnChunk(ref chunk);        
    
        stream.Seek(offsetOfChunkInFile, SeekOrigin.Begin);
        stream.Write(chunk, 0, numOfBytesInChunk);
    }
    
    private void DoWorkOnChunk(ref byte[] chunk)
    {
        //TODO: Any mutation done here to the data in 'chunk' will be written out to the stream.
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the
I want use html5's new tag to play a wav file (currently only supported
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.