Drew Birmingham
Drew Birmingham

Reputation: 55

C# Reading, Modifying then writing binary data to file. Best convention?

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:

Upvotes: 3

Views: 7033

Answers (1)

Lars Kemmann
Lars Kemmann

Reputation: 5674

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.
}

Upvotes: 3

Related Questions