I'm currently reading a file in through the binaryreader class and I can successfully write it back out using the binarywriter class.
What I am trying to find out is how to insert data into the file. Whether reading it all in, inserting what I need to and recreating it byte for byte I don't mind.
binarywriter doesn't do what I need to do as it doesn't insert, only writes over.
What options do I have on inserting data (shorts/bytes ect) into a file.
Loading
VulpesPosted May 26, 2013, 11:42 AM
You could then use the appropriate BinaryWriter method to make the insertion (Write(Int16) for a short) and then carry on reading the existing bytes and writing them to the second file until you get to the end.
You'd then need to delete the first file and rename the second file to the first.
This is not as neat but has the advantage that you don't need to read the entire file into memory first.
Jay SPosted May 26, 2013, 11:33 AM
Is putting each byte into a list the only way or are there alternatives?
VulpesPosted May 26, 2013, 11:26 AM
As it's a List of bytes, the index into the List will be the same as the offset into the file - whether this is expressed as decimal or hexadecimal.
So, to insert 65 at offset 0x1F, you'd do:
bytes.Insert(0x1F, (byte)65);
Jay SPosted May 26, 2013, 10:53 AM
If I read it in byte by byte into a list, what if I want to add a short?.
Also, by reading it into a list as suggested, and then going to index 5 to insert, what if I only know the hex/decimal offset where I want to insert rather than the lists index?
VulpesPosted May 24, 2013, 7:08 PM