Implement methods that insert or delete space at any place in file (affecting file length), using technique that copies content within file so there is no need for temporary file.
When inserting space of N bytes:
1. Extend file size by N bytes
2. Copy/shift all data after insertion position, N bytes towards file's end
1. Copy/shift all data after deletion position + N bytes, N bytes towards file's beginning
2. Shrink file size by N bytes
Framework and system file API offer functions to write new file or append to existing, there are no functions to insert new data (and not overwriting existing) in existing file at some arbitrary position e.g. beginning of file or middle of file. Also, there are no functions for removing data in similar way.
Usual solutions that I've seen tend to (to simulate insert):
- make temporary file
- append data before insert position from original file
- append data to insert
- append data after insert position from original file
- delete original file
- rename temporary file to original file's name
Problem with that approach is:
- choosing name for temporary file
- need for disk space for original and temporary file
- copying of original data positioned before insert/delete position (no need for that)
- Each step works on resulting data from previous step.
- Inserted part can be also interpreted as unchanged data, but logically it is treated as uninitialized as it is expected that user will write new data after the part is inserted.
Initial file stream (letter 'A' is at position 0)
FileStream file = new FileStream("test.txt", FileMode.Create, FileAccess.ReadWrite); byte initial = Encoding.ASCII.GetBytes("ABCDEFGHIJKLMNOPQRSTUVWXYZ"); file.Write(initial, 0, initial.Length);
Insert (make room for) 4 bytes at position 7(letter 'H')
Quazistax.QSFile.InsertFilePart(file, 7, 4);
Fill 4 bytes at position 7 with '='
Quazistax.QSFile.FillFilePart(file, 7, 4, (byte)'=');
Delete 7 bytes at position 16 (part 'MNOPQRS', letter 'M' is at position 16)
Quazistax.QSFile.DeleteFilePart(file, 16, 7);
Copy 8 bytes from position 5 (letter 'F') to position 14 (letter 'K')
Quazistax.QSFile.CopyFilePart(file, 5, 14, 8);
You can download the source here.